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:
InstanceId
services provide APIs to generate gcm registration tokens
You send & store this generated token in your app server. 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 ) 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:
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. 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? 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? 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
上一篇: Swift:防止ABPeoplePickerNavigationController关闭
下一篇: 困惑于GCM令牌更新过程