我开发了一个使用 GCM 技术的应用程序,一切正常。
我观察到设备的注册 ID 在一段时间后发生变化,这导致我的应用出现问题,因为我的应用依赖于注册 ID。
那么我如何才能为客户获得一个固定的注册 ID?
最佳答案
我阅读了 2 个原因 here当您的 GCM 注册 ID 可能更改时:
- 每次更新您的设备时,您都需要重新注册每台设备 应用程序。
- 如果运行的 Android 版本已更新,您还需要重新注册设备
附言:以下旧答案的引用已从 Google 的页面中删除,因此可能不再有效
如果您在 Architectural Overview 上的启用 GCM 标题下看到第二个点页面,它说:
Note that Google may periodically refresh the registration ID, so you should design your Android application with the understanding that the com.google.android.c2dm.intent.REGISTRATION intent may be called multiple times. Your Android application needs to be able to respond accordingly.
因此,为了处理这个问题,您应该有一个 Broadcast Listener
可以处理 com.google.android.c2dm.intent.REGISTRATION
intent,Google 将其发送到应用程序当它必须刷新注册 ID 时。广播接收器将具有带有 Intent 的 onReceive
方法。从 Intent 中,您可以获得 Bundle
,您可以使用它从 Google 提取新的注册 ID。您可以保存它并将其发送到第三部分服务器以替换您之前为该用户注册的 ID。
您还可以看到 this回答问题In GoogleCloudMessaging API, how to handle the renewal or expiration of registration ID? .
关于 Should applications call gcm.register() every seven days to ensure valid registration IDs? 的讨论问题也可能有用。
希望这可以帮助您了解如何处理它。
关于android - GCM 注册 ID 已更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17893117/