River Paul
River Paul

Reputation: 144

Firebase Cloud Messaging for Web - How to maintain the token list in the database and ensure they are valid or up-to-date

With Firebase Cloud Messaging for Web,

How do I maintain the list of valid tokens in my database? For example I've noticed when a user turns off notifications and revisits the site, a new token will be generated and the old token in my database is useless.

I've also tried using Firebase messaging.onTokenRefresh() callback, but it does not get called when I turned off notifications. Also in this case, even if it did get triggered, it returns a new token that was refreshed. How do I keep track of the old token that was refreshed?

Can someone please share with me their thoughts/ways to maintain and ensure the token list in the database are valid or up-to-date?

Any feedback is much appreciated.

Thank you,
Christina

Upvotes: 1

Views: 561

Answers (1)

collimarco
collimarco

Reputation: 35400

messaging.onTokenRefresh() is probably a wrapper around the event onpushsubscriptionchange.

Indeed that event is currently only called when the subscription is enabled (or enabled again), but not when the permission for push notifications is revoked. So at the moment you can only know that an endpoint has expired when you try to send a notification to it.

More details: http://blog.pushpad.xyz/2016/05/the-push-api-and-its-wild-unsubscription-mechanism/

In any case you can use the callback to send any new token to the server: at first you will have two tokens stored for the same browser, one expired and the other valid.

Some problems arise if you have data associated to the endpoint (e.g. tag) that you want to preserve during the endpoint change: see the blog post for some suggestions.

Upvotes: 1

Related Questions