Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Cloud Messaging - Managing Registration Tokens

I'm looking at implementing messaging between mobile and browser apps using Firebase cloud messaging and i have a few questions, that the docs don't seem to answer.

For being able to receive messages, you need a Registration Token (RT). Messages can be send to a RT, to a topic or to a device group notification_key.The RT can also be used:

  • directly
  • to subscribe to a topic
  • to add to a device group

The RT can also expire/change.

In my app, I'm maintaining a list of RT per user. Now, when the RT changes:

  1. Do I have to unsubscribe the old token and subscribe the new token to topics?
  2. Do I have to remove the old token and add the new token to device groups?
  3. Is it possible to get information about device groups/topics for a token?
  4. Can I add a token to a device group more than once?
  5. Can I subscribe a token to a topic more than once?
  6. Will multiple subscriptions/additions of the same token result in receiving duplicate messages

Sorry, that's a lot of questions, but I guess, for somebody who has gone through this, it should be pie ;)

like image 877
Ralf Bokelberg Avatar asked Feb 15 '17 11:02

Ralf Bokelberg


People also ask

What is registration token in Firebase?

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices or create device groups, you'll need to access this token. You can access the token's value by extending FirebaseInstanceIdService.

Does FCM registration token change?

Similarly to how GCM works, the FCM token can change due to token-rotation. Note: the token rotation is a rare-event. Don't expect to see it often. But still, if you care about the token you should implement onTokenRefresh() to be informed of changes.


1 Answers

  1. Do I have to unsubscribe the old token and subscribe the new token to topics?

AFAIK, you don't have to unsubscribe the old token, since it will be discarded by FCM itself. For the new token, yes, you'll have to subscribe it to the topic you need. The usual thing is done (in Android) by having subscribeToTopic() in onTokenRefreshed().


  1. Do I have to remove the old token and add the new token to device groups?

Yes. You have to handle the mapping/relationships for Device Group Messaging. See my answer here. This is different from topics. The token will be invalidated, but will be kept as part of the list of registration tokens for the corresponding registration key.

It's why there's a possibility to receive a NotRegistred error on one of the tokens if you send to Device Group. :)


  1. Is it possible to get information about device groups/topics for a token?

For Device Group Messaging (same with #2), the developer (you) have to manage these details yourself. For topics, you can use the InstanceID API. Specifically, set details parameter to true:

[optional] boolean details: set this query parameter to true to get available IID token details, including connection information and FCM or GCM topic subscription information (if any) for the device associated with this token. When not specified, defaults to false.


  1. Can I add a token to a device group more than once?

Ahmm. Yes. Do you mean the same token? If so, I haven't tried it yet. Might as well do some checking on the client side before adding.


  1. Can I subscribe a token to a topic more than once?

If you mean re-subscribing, then yes. If you mean duplicate request to subscribe, I think the result would still be a success. No changes in behavior though.


  1. Will multiple subscriptions/additions of the same token result in receiving duplicate messages?

Tested it out. You won't receive duplicate messages for both duplicate topic subscriptions and adding the same token to a device group. It seems that FCM ignores the request to subscribe/add a Registration token if it's already subscribed/added to a device group.

like image 106
AL. Avatar answered Sep 22 '22 09:09

AL.