问题描述
我正在关注集成示例来自官方指南。
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.
现在我每次启动主要活动时都会调用intent服务,并且我相信instanceID负责启动令牌刷新。
Now I call the intent service each time my main activity launches and I believe that instanceID is responsible for initiating the token refresh.
每次从我的主要活动启动此GCM注册意向时,我应该检查Shared Prefs值。但是在这种情况下刷新将失败,因为在初始令牌获取之后,条件将始终为真。
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.
我应该放弃共享prefs逻辑 - 这种方式每次都会将新令牌发送到我的服务器。这样做的正确方法是什么?令牌如何刷新worrk以及它何时刷新?
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?
推荐答案
是的,您不需要将其保存在sharedPreference中。为了保护客户端应用程序和应用程序服务器免受注册令牌的潜在恶意重用,您应该定期从服务器启动令牌刷新。当从服务器端启动GCM注册令牌刷新时,客户端应用程序必须处理带有GCM注册客户端/服务器握手的tokenRefreshed消息。
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.
基于,备份返回的Google Cloud Messaging注册可能导致的注册令牌意外的行为通知恢复的应用程序。这是因为,当用户将您的应用安装在新设备上时,应用必须向GCM API查询新的注册令牌。如果旧注册存在,因为系统已将其备份并恢复,则该应用程序不会查找新的令牌。为防止此问题出现,请将注册令牌从备份文件集中排除。
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.
以下是Google服务的演示应用程序 - MyInstanceIDListenerService:
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
有关详情,请阅读官方Google文档:
For more information, please read the Official Google Documentation here: https://developers.google.com/cloud-messaging/registration
这篇关于GCM令牌刷新以及何时将令牌发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!