我正在开发一个使用 gcm 的 android 项目。我开始知道,如果我们从设备上卸载应用程序并在大多数情况下重新安装,设备会获得新的注册 ID。然后,如果我们不从应用程序服务器中删除旧注册 ID 并进行更新,消息将同时发送到两个 ID 和响应将显示规范 ID 存在。我的问题是,此时消息是否会成功发送到该设备?
最佳答案
当您在 Google 的响应中收到规范的注册 ID 时,该消息已被 GCM 服务器接受,并且 GCM 服务器会尝试将其传送到设备。它是否实际发送到设备取决于设备是否可用(即连接到互联网)。 因此,如果您的服务器向旧 ID 和新 ID 发送 GCM 消息,设备可能会收到两条消息。
Canonical IDs
On the server side, as long as the application is behaving well, everything should work normally. However, if a bug in the application triggers multiple registrations for the same device, it can be hard to reconcile state and you might end up with duplicate messages.
GCM provides a facility called "canonical registration IDs" to easily recover from these situations. A canonical registration ID is defined to be the ID of the last registration requested by your application. This is the ID that the server should use when sending messages to the device.
If later on you try to send a message using a different registration ID, GCM will process the request as usual, but it will include the canonical registration ID in the registration_id field of the response. Make sure to replace the registration ID stored in your server with this canonical ID, as eventually the ID you're using will stop working.
( Source )
您可以通过为应用程序的每个实例分配一个唯一标识符来解决这个问题。如果您将该标识符存储在设备的外部存储中,则在卸载应用程序时它不会被删除。然后您可以在再次安装该应用程序时恢复它。如果您将此标识符与注册 ID 一起发送到您的服务器,您可以检查您的服务器是否有此标识符的旧注册 ID,并将其删除。
关于android - gcm canonical id 是否应该更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23710404/