Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what period does the firebase's app token changes and how to manage it?

I am new to firebase I am learning it like a toddler learning to walk. So far I have managed to send a message to my own phone using a token generated for my phone by firebase framework. Now here's where it gets tricky (in my opinion). There is a method called onTokenRefresh() in the FirebaseInstanceIdService extended service. Since it is called refresh, Then I am assuming that it will change. I want to know when this token is created and when will it be changed?

And if it changes, suppose I send a message to a device with token 'A' which is offline for now, so it will be queued. Now when the device gets online, it will "refresh" the token to 'B'. Now as the message was supposed to be delivered to token 'A', the message will never be delivered. How can I manage this situation?

like image 703
roostaamir Avatar asked Jun 07 '16 11:06

roostaamir


People also ask

How often do FCM tokens change?

To cover all cases, you should adopt a threshold for when you consider tokens stale; our recommendation is two months. Any token older than two months is likely to be an inactive device; an active device would have otherwise refreshed its token.

How long does a firebase token last?

The Firebase Admin SDK has a built-in method for creating custom tokens. At a minimum, you need to provide a uid , which can be any string but should uniquely identify the user or device you are authenticating. These tokens expire after one hour.

Does firebase refresh token expire?

Firebase ID tokens are short lived and last for an hour; the refresh token can be used to retrieve new ID tokens. Refresh tokens expire only when one of the following occurs: The user is deleted. The user is disabled.

How can I check my FCM token?

Check the format of the registration token you pass to the server. Make sure it matches the registration token the client app receives from registering with Firebase Notifications. Do not truncate or add additional characters. Notice that I added in "ABC" , in the registration_ids parameter.

How to get device token from Firebase instance?

and to get the device token at any time we can use FirebaseInstanceId.getInstance ().getToken () method to get the current device token.It takes a bit of time to get the device token. Click here to read more about accessing device registration token. onTokenRefresh () and FirebaseInstanceIdService are deprecated.

What happens to the old registration token when the app refresh?

Finally, even after the refresh happened the old token will still be working for a short period, to allow the app to communicate the new token to its back-end. On initial startup of your app, the sdk of FCM generates the registration token for the client app instance.

When will the token be generated for the mobile app?

The token is generated, after the app is first launched, as soon as the phone can connect to the Google servers. Due to the required connectivity this might not happen immediately, but in most of the cases it will happen in few seconds after the user open the app.

How does FCM generate registration token for a client app?

On initial startup of your app, the sdk of FCM generates the registration token for the client app instance. As above said, It is a rare event. To be specific,The registration token may change when:


1 Answers

The token is generated, after the app is first launched, as soon as the phone can connect to the Google servers. Due to the required connectivity this might not happen immediately, but in most of the cases it will happen in few seconds after the user open the app. As soon as the token is generated the method onTokenRefresh() is called.

As you pointed out the token can change, in which case the onTokenRefresh() method will be called again.
The refresh event is somehow rare, don't expect to see it often at all.

When the refresh token happens, all the messages that have been "successfully" sent (the API returned you a message-id) to the old token will be delivered.

Finally, even after the refresh happened the old token will still be working for a short period, to allow the app to communicate the new token to its back-end.

like image 80
Diego Giorgini Avatar answered Sep 23 '22 17:09

Diego Giorgini