Do I need to extend FirebaseInstanceIdService to subscribe to FCM topics?

I want to manage a theme subscription from a client (Android app). I am currently doing this while working onCreate (). I am wondering if it is right to subscribe / unsubscribe to InstanceIdService :: onTokenRefresh () or at any convenient time (when a button is clicked, etc.).

In short, if I manage a client-side topic subscription (without a server), do I still need to worry about InstanceIdService?

Different documentation sources provide different options for subscribing to Firebase Cloud Messaging (FCM) topics. Some mention InstanceIdService, some do not. Here they are:

He does not mention InstanceIdService when discussing topic subscription.

After completing the configuration tasks, you can add the client code to subscribe to the topic, and then process the messages sent to the topic.

Client applications can subscribe to any existing topic, or they can create a new theme. When a client application subscribes to a new theme name (does not yet exist for your Firebase project), a new theme of this name is created in FCM, and any client can subsequently subscribe to it.

To subscribe to a topic, the client application calls Firebase Cloud Messaging subscribeToTopic () with the theme name FCM:

FirebaseMessaging.getInstance().subscribeToTopic("news"); 
  1. Firebase Android Codelab

The MyFirebaseInstanceIdService class will be the service used to process FCM logic. This service is used to notify the application when a new InstanceID is generated and the generated token is retrieved.

Modify it to extend FirebaseInstanceIdService and override onTokenRefresh to subscribe to the topic. Use the following code to update the onTokenRefresh method in MyFirebaseInstanceIdService to look like this:

 public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService { private static final String TAG = "MyFirebaseIIDService"; private static final String FRIENDLY_ENGAGE_TOPIC = "friendly_engage"; /** * The Application current Instance ID token is no longer valid * and thus a new one must be requested. */ @Override public void onTokenRefresh() { // If you need to handle the generation of a token, initially or // after a refresh this is where you should do that. String token = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "FCM Token: " + token); // Once a token is generated, we subscribe to topic. FirebaseMessaging.getInstance() .subscribeToTopic(FRIENDLY_ENGAGE_TOPIC); } } 
  1. Quickstart firebase project on github

It uses InstanceIdService, but the topic of the subscription does not occur there. This is done simply in the client as part of a button click in action:

 Button subscribeButton = (Button) findViewById(R.id.subscribeButton); subscribeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // [START subscribe_topics] FirebaseMessaging.getInstance().subscribeToTopic("news"); // [END subscribe_topics] // Log and toast String msg = getString(R.string.msg_subscribed); Log.d(TAG, msg); Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show(); } }); 

Comment in code InstanceIdService offers the manager a subscription onTokenRefresh()

 @Override public void onTokenRefresh() { // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Refreshed token: " + refreshedToken); // If you want to send messages to this application instance or // manage this apps subscriptions on the server side, send the // Instance ID token to your app server. sendRegistrationToServer(refreshedToken); } 
+6
source share
1 answer

As you already know, FirebaseInstanceId is probably a singleton class where you retrieve your registration token. Therefore, I think that the subscribeToTopic() method, since only the topic name is transmitted, you can assume that it already calls an instance of FirebaseInstanceId itself or simply sends a request to FCM servers with the corresponding registration token and the topic that it should subscribe to.

In short, I don’t think you need to extend FirebaseInstanceIdService to call subscribeToTopic() , however I find this necessary because (as described in docs ):

Base class for handling Firebase event id token update events.


For other questions.

I want to manage a theme subscription from a client (Android app). I am currently doing this while onCreate() active. I am wondering if it is right to subscribe / unsubscribe to InstanceIdService::onTokenRefresh() or at any convenient time (when you click a button, etc.)?

I think doing it in onCreate() is ok. If you see my answer here , @FrankvanPuffelen mentioned:

subscribing to topics at the beginning of the application is fine.

However, I think it is also useful to add a subscribeToTopic() call to onTokenRefresh() , so that as soon as a new token is provided for the application instance, you will immediately sign it accordingly.

Note This behavior I'm thinking about right now is that when the registration token is invalid, its subscription is also lost and that adding subscribeToTopic() to onRefreshToken() will immediately rewrite them for you (of course, this still depends on your implementation what topic you want to subscribe to).

+2
source

Source: https://habr.com/ru/post/1011474/


All Articles