12

I'm newly learning android studio, so however I have one doubt in developing a android app that:
How can we subscribe to multiple topics in FCM?

Here is my code

public void subscribeToPushService() {
        FirebaseMessaging.getInstance().subscribeToTopic("News");

        Log.d("myname", "Subscribed");
        // Toast.makeText(Simple.this, "Subscribed", Toast.LENGTH_SHORT).show();

        String token = FirebaseInstanceId.getInstance().getToken();

        // Log and toast
        Log.d("myname", token);
        // Toast.makeText(Simple.this, token, Toast.LENGTH_SHORT).show();
    }

you can see in the above code I have already subscribed to one topic "News" so

how can I subscribe to multiple topics instead of one?
Is it possible, if yes how can we do that?

greybeard
  • 2,249
  • 8
  • 30
  • 66

1 Answers1

14

Yes,find below sample code for subscribe multiple topics

public void subscribeToPushService() {
        FirebaseMessaging.getInstance().subscribeToTopic("News");
        FirebaseMessaging.getInstance().subscribeToTopic("Movies");
        FirebaseMessaging.getInstance().subscribeToTopic("etc");
}

For unsubscribe topic

FirebaseMessaging.getInstance().unsubscribeFromTopic("Topic name");
Kanaiya Katarmal
  • 5,974
  • 4
  • 30
  • 56