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");
- 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"; @Override public void onTokenRefresh() {
- 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) {
Comment in code InstanceIdService offers the manager a subscription onTokenRefresh()
@Override public void onTokenRefresh() {