android - 对 GCM token 更新过程感到困惑

标签 android google-cloud-messaging

尝试实现谷歌最新的 GCM 服务。我读过GCM documentation .我也下载并分析了google's sample implementation .综上所述,我明白了以下几点:

  1. InstanceId 服务提供 API 来生成 gcm 注册 token 您发送生成的 token 并将其存储在您的应用服务器中。
  2. 这些 token 可以从客户端和 instanceId 服务端偶尔更改一次,如前所述 here .要处理此问题,您必须实现 InstanceIDListenerService 并且 InstanceID 提供程序将调用 onTokenRefresh,您只需在其中编写逻辑以获取新 token 并将其发送到服务器(Google's sample app )
  3. 如果您的应用服务器发送较旧的注册 ID,GCM 服务器会向您的设备发送一个名为 canonical_id ( as mentioned here ) 的东西(这是从设备发送的最后一个 registration_id)。您必须使用此 canonical_id 替换服务器中的现有 token 。

现在,以下是我的问题:

  1. InstanceId.getToken 如果应用程序未卸载,它似乎返回相同的 token ,如果 token 未更改,它返回得非常快。那么,我可以在每次启动应用程序时调用 RegistrationIntentService 吗?这样我就可以保证始终使用最新的 token 。
  2. onTokenRefresh 如果在您的应用未连接到 Play 商店(没有互联网或其他东西)时发生刷新,如何执行? InstanceId 提供商是否重试?这在某处记录了吗?如果同时发送推送通知会怎样?
  3. canonical_id 到底是什么?它是为设备生成的最新 token (由 InstanceID.getToken 在客户端或 InstanceId 提供商端启动)吗?如果 canonical_id 确实是最新的 gcm token ,那么 onTokenRefresh 实现的需求是什么,因为您无论如何都可以分析推送通知数据并在找到 时更新您的应用服务器>canonical_id 提供了吗?

最佳答案

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

更好的解决方案是优先保存您已经设法注册 token 的信息。仅当您尚未注册时才启动 RegistrationIntentService

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

然后当您启动您的应用时,只需检查 PREFIX_PREF_GCM_KEY 是否为真

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

我猜这取决于系统调用此刷新过程。文件指出:

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.

它可以在您的应用休眠时调用(与您收到通知时相同),但您应该对其进行测试并查看它是否按预期工作。

我还认为您可以假设当没有互联网连接时,System 不会调用 onRefreshToken,原因很简单,它无法接收更新通知...但一如既往,您应该自行测试更新过程是否有效以及在何种条件下有效。

What exactly is a canonical_id?

您可能错误地在您的 服务器中为同一设备注册了多个注册 ID - 例如 - onRefreshToken - 注册了一个 token 但没有删除旧的。 如果您要使用旧的 registartaion_id 发送消息,Google 会通知您应该将其更改为新的 - canonical_id

关于android - 对 GCM token 更新过程感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32164880/

相关文章:

java - 数据绑定(bind),MaterialCardView 应该像 Radiogroup

向 Gcm 发送 1000 多个用户的 Php 函数

android - 使用 JSON 向 GCM 发送多通知

android - 如何检测您的 Android 应用程序是否在前台(API 21)

android - GCM getToken() 发送 java.io.IOException : TIMEOUT

java - AdMob 加载测试广告而非真实广告

java - Android JSON 与 PHP,带重音的字符未正确处理

android - 底部在 flutter 中重载了 213 像素

安卓设备配置问题

android - 由于 GCM SenderId Android 无法生成 APK 版本