Andrew Stephens
Andrew Stephens

Reputation: 10191

Publishing/subscribing to FCM topics

I've never used FCM (or GCM) before, but I'm looking into writing an app that will involve publishing messages to multiple devices, and "topics" seem to be a good approach.

From what I've read of the docs so far, my app server will publish messages to a named topic, so presumably it doesn't need to know about registration tokens for the recipient devices?

I believe a registration token must be specified when subscribing to a topic, so I guess a recipient device must still request a token before it can subscribe. What happens when the token expires - does the app have to subscribe to the topic again, using the new token?

Upvotes: 1

Views: 679

Answers (1)

AL.
AL.

Reputation: 37778

so presumably it doesn't need to know about registration tokens for the recipient devices?

No. It doesn't. But I would strongly suggest for you to save the registration tokens so you can keep track of them using the Diagnostics tool in the future.

What happens when the token expires - does the app have to subscribe to the topic again, using the new token?

In Android, you simply have to call

FirebaseMessaging.getInstance().subscribeToTopic(<topic_name_here>);

to subscribe the device to a topic. The way on how I understand this works is explained here, where the FirebaseMessaging class calls an instance of the FirebaseInstanceId and (presumably) subscribe the currently active registration token.

According to @DiegoGiorgini's answer here, the topic subscriptions are maintained if the token is refreshed.

AFAIK (haven't encountered it yet), the corresponding registration token should be re-subscribed. Depending on where you implement the subscription.

I would suggest having it on start of the app itself. Better if you'd have a list of topics (should there be more than one) on your App Server, and perhaps make use of the InstanceID API to do the re-subscription if needed.

Upvotes: 1

Related Questions