Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When and what Old FCM Tokens do I need to delete from my backend?

The way I understand Firebase Cloud Messaging at the moment, I will want to save all FCM tokens a user might have across devices to my backend and send a notification to all of these tokens at once when I want to notify that user about something.

Multiple tokens

Until this point, I assumed that each device only has one active token, however, reading this section of the documentation:

To enable this feature, make sure you have each sender's sender ID. When requesting registration, the client app fetches the token multiple times, each time with a different sender ID in audience field, using the token retrieval method for the given platform

Does this mean that I might need to target multiple active tokens per device?

Deletion

And now coming to the really important question. A simple solution to the above problem would be to simply store every token I ever retrieve in the backend and thus ensuring that my user will always receive the message.

However, what do I do if the user signs out of my app using Firebase Authentication, i.e. a different account is used in the same app on the same device?
I assume that the tokens I sent to my backend for this device will still be active - so now this user will receive notifications from another account because that account was signed in on the device previously.
I do have access to the current FCM token and I could delete that from my backend before signing out the old user, but considering the "Multiple tokens" section: how do I make sure that I can delete all FCM tokens of the old user from my backend?

Uniqueness

Additionally, assuming that old tokens are just dead for the device (will not trigger notifications anymore) when new ones are generated, can I be sure that this token will never be assigned to another device in the future?

TL;DR

How can I make sure that I have the correct FCM token(s) for my user stored in my backend and more importantly: how can I ensure that no tokens of other users are saved for some user in my backend?

I read through:

  • https://stackoverflow.com/a/40158260/6509751

However, I still do not know how to deal with multiple tokens.

like image 943
creativecreatorormaybenot Avatar asked Nov 07 '22 12:11

creativecreatorormaybenot


1 Answers

Does this mean that I might need to target multiple active tokens per device?

An application has a single active token for each sender ID. It's fairly uncommon to have multiple sender ID, and you'd usually know if if you do. If you're sending from a single back-end, there's usually no need for having multiple sender IDs.

like image 148
Frank van Puffelen Avatar answered Nov 15 '22 06:11

Frank van Puffelen