bs140200456 Aqsa Anum bs140200456 Aqsa Anum - 25 days ago 17
reST (reStructuredText) Question

Firebase: Is this correct way to create and subscribe a topic for firebase console?

I have connected my project to firebase and I want to receive notifications on this app even if closed, in background, foreground or killed.
I have made one button, when clicked it will subscribe user to the news topic, so can anyone please tell me that is this correct way to make a topic ?

public void onClick(View view){

FirebaseMessaging.getInstance().subscribeToTopic("news");
}

Answer Source

Here's what the docs say :

Based on the publish/subscribe model, FCM topic messaging allows you to send a message to multiple devices that have opted into a particular topic. You compose topic messages as needed, and FCM handles routing and delivering the message reliably to the right devices.

For example, users of a local weather forecasting app could opt into a "severe weather alerts" topic and receive notifications of storms threatening specified areas. Users of a sports app could subscribe to automatic updates in live game scores for their favorite teams.

So calling subscribeToTopic is the correct way to subscribe the user to the Messaging Topic. You need to also extend the FirebaseMessagingService overriding onMessageReceived and onDeletedMessages.

The case when the app is in foreground or background:

  • Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
  • Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray and the data payload is delivered in the extras of the intent of your launcher Activity.