Android: Subscribe to Firebase Cloud Messaging(FCM) Topic

1. How can I understand that subscription was successful?

Edit:

You could now check if subscription is successful by adding addOnSuccessListener()

FirebaseMessaging.getInstance().subscribeToTopic("news").addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show();
        }
    });

Original:

There is nothing explicitly mentioned in the docs about a response received when the subscription is successful.

However, if you need to mandate all of your users to be subscribed to a specific topic, you should call the subscribeToTopic on your app's first install. This will most likely make sure that there is a connection to the internet (since it's probably been downloaded and installed via the Play Store) and the subscription successful.

However, if you want to make sure, you can also handle he checking via your own App Server. As mentioned in the docs:

You can take advantage of Instance ID APIs to perform basic topic management tasks from the server side. Given the registration token(s) of client app instances, you can do the following:

  • Find out details about a client app instance's subscriptions, including each topic name and subscribe date. See Get information about app instances.

Check through the registration tokens, if they haven't been successfully subsribed to your topic, send a notification to it where it will trigger your client app to call subscribeToTopic.

2. Is it a bad practice to call subscribeToTopic each time my application starts?

Edit: Adding it in from the comments section: Subscribing on app start should be fine.

Thank you @FrankvanPuffelen for verifying. :)


I have written this function and tested. May be helpful.

    private void subscribeToMessaging(){
        SharedPreferences prefs = getSharedPreferences(SETTINGS_TITLE, MODE_PRIVATE);

// Getting value from shared preferences
        boolean isSubscriptionEnable = prefs.getBoolean(SETTING_NOTIFICATION, true);

// if "isSubscriptionEnable" is true then check whether its already subscribed or not
        if (isSubscriptionEnable){

            boolean alreadySubscribed = prefs.getBoolean(SETTING_ALREADY_SUBSCRIBED, false);
// if not already subscribed then subscribe to topic and save value to shared preferences
            if (!alreadySubscribed){
                FirebaseMessaging.getInstance().subscribeToTopic("global").addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show();
        }
    });

                SharedPreferences.Editor editor = getSharedPreferences(SETTINGS_TITLE, MODE_PRIVATE).edit();
                editor.putBoolean(SETTING_ALREADY_SUBSCRIBED, true);
                editor.apply();
                Toast.makeText(this, "Subscribed", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "Already subscribed", Toast.LENGTH_LONG).show();
            }
        }
    }

Don't forget to write these lines above onCreate()

    public static final String SETTINGS_TITLE = "settings";
    public static final String SETTING_NOTIFICATION = "notification_state";
    public static final String SETTING_ALREADY_SUBSCRIBED = "already_subscribed";