Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about GCM Token updation process

Trying to implement google's latest GCM services. I have read GCM documentation. I have also downloaded & analysed google's sample implementation. From all the above I understood the following:

  1. InstanceId services provide APIs to generate gcm registration tokens You send & store this generated token in your app server.
  2. These tokens can be changed once in a while from client side as well as instanceId service side as mentioned here. To handle this, you have to implement InstanceIDListenerService and the InstanceID provider will call onTokenRefresh where you just write your logic to get a new token and sending it to server (Google's sample app )
  3. There's something called canonical_id (as mentioned here) (which is the last registration_id send from a device) that GCM server sends your device if your app server sends an older registration id. You have to replace your existing token in server with this canonical_id.

Now, following are my questions:

  1. InstanceId.getToken seems to return same token if the app is not uninstalled and it returns pretty fast if the token hasn't changed. So, can I call the RegistrationIntentService every time I start the app? This way I'm guaranteed to get to work with the latest token all the time.
  2. How does the onTokenRefresh if refresh happens while your app is not connected to the play store (no internet or something)? Does InstanceId provider re-try? Is this documented somewhere? What happens if at the same time a push notification is sent?
  3. What exactly is a canonical_id? is it the latest token generated for a device (initiated by InstanceID.getToken at client or at InstanceId provider end)? If canonical_id is indeed the latest gcm token, what is the need of onTokenRefresh implementation as you can anyway analyse the push notification data and update your app server if you find a canonical_id provided?
like image 499
ranjjose Avatar asked Aug 23 '15 08:08

ranjjose


People also ask

Is GCM still working?

On April 10, 2018, Google deprecated GCM. The GCM server and client APIs were removed on May 29, 2019, and currently any calls to those APIs can be expected to fail. Google Cloud Messaging, deprecated April 10 2018, has been deactivated and removed from Google's APIs.

What is GCM registration ID?

The GCM Registration ID is a randomly-generated identifier that does not contain any personal or device information that could allow a developer to discover the personal identity of the user.

What is GCM token?

These tokens are used to identify the InstanceID and can expire and be refreshed. On the client device, you initialize an InstanceID, then with that InstanceID you generate a token (registration token). You send that token to your server, which uses the token to send messages to the InstanceID (installed application).


1 Answers

can I call the RegistrationIntentService every time I start the app?

A better solution would be to save in preference that you already managed to register a token. start RegistrationIntentService only if you didn't already register.

String token = InstanceID.getToken(...);
//send to server
getSharedPreferences(context).edit().putBoolean(PREFIX_PREF_GCM_KEY, true).apply();

then when you start your app just check if PREFIX_PREF_GCM_KEY is true

How does the onTokenRefresh if refresh happens while your app is not connected to the play store (no internet or something)

I'm guessing that it's up to the system to call this refresh procedure. the documentation states:

Called when the system determines that the tokens need to be refreshed. The application should call getToken() and send the tokens to all application servers. This will not be called very frequently, it is needed for key rotation and to handle special cases. The system will throttle the refresh event across all devices to avoid overloading application servers with token updates.

It can be called while your app is asleep (same as when you are getting notification) but you should test it and see that it's working as expected.

I also think that you can assume that while there is no internet connection, the System will not call onRefreshToken for the simple reason that it won't be able to receive update notifications... but as always you should test by yourself to see if the update process works and in which conditions.

What exactly is a canonical_id?

It's possible that by mistake you have registered multiple registration id's for the same device in your server - for example - onRefreshToken - registered a token without deleting the old one. If you will send a message using the old registartaion_id, google will let you know you should change it to the new one - the canonical_id

like image 174
royB Avatar answered Sep 24 '22 00:09

royB