GCM token refresh and when to send the token to server

只谈情不闲聊 提交于 2019-12-22 08:17:20

问题


I am following a GCM-Android integration example from the official guide.

In particular I am confused about the following lines in the above linked class:

// You should store a boolean that indicates whether the generated token has been
// sent to your server. If the boolean is false, send the token to your server,
// otherwise your server should have already received the token.

Now I call the intent service each time my main activity launches and I believe that instanceID is responsible for initiating the token refresh.

Should I check the Shared Prefs value each time I initiate this GCM registration intent from my Main Activity. However refresh will fail in this case because after initial token fetch the condition will always be true.

Should I discard the shared prefs logic - this way a fresh token will be sent to my server each time. What is the proper way of doing this? How does the token refresh worrk and when does it refresh?


回答1:


Yes, you don't need to save it in sharedPreference. To protect the client app and app server from potential malicious re-use of registration tokens, you should periodically initiate token refresh from the server. When GCM registration token refresh is initiated from server side, the client app must handle a tokenRefreshed message with the GCM registration client/server handshake.

Based on the document, backing up the registration token that Google Cloud Messaging registration returned can cause unexpected behavior in notifications for the restored app. This is because when a user installs your app on a new device, the app must query the GCM API for a new registration token. If the old registration is present, because the system had backed it up and restored it, the app doesn't seek the new token. To prevent this issue from arising, exclude the registration token from the set of backed-up files.

Here's a demo app for Google Services - MyInstanceIDListenerService: https://github.com/googlesamples/google-services/blob/master/android/gcm/app/src/main/java/gcm/play/android/samples/com/gcmquickstart/MyInstanceIDListenerService.java#L38

For more information, please read the Official Google Documentation here: https://developers.google.com/cloud-messaging/registration



来源:https://stackoverflow.com/questions/35357131/gcm-token-refresh-and-when-to-send-the-token-to-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!