我目前正在尝试使用 GCM 向用户发送通知,目前我仍在研究如何最大限度地利用它。现在我只使用文档中提供的示例项目 here我使用 gcm-client 示例来处理它。
现在使用这个 project from Git我尝试使用应用程序创建的注册 ID 推送消息,是的,它成功发送了消息。
现在的问题是在我卸载应用程序之后。在我重新安装它之后,它将生成一个新的注册 ID,其中我将它与以前的注册 ID 一起存储在服务器上,除了我无法标记以前的注册 ID 以不再接收任何消息,因为当用户没有互联网时可能会发生卸载联系。之后,我向两个注册 ID 发送消息,即卸载应用程序之前的 ID 和重新安装应用程序后的 ID。结果是我收到了两条推送消息,尽管我预计它只会收到一条消息,因为应用程序已经更改了注册 ID。
如果我也更新了该应用程序,我预计该应用程序可能会收到两个或更多重复的应用程序,因为如文档中所述,注册 ID 可能会在更新时发生变化。
我可以采取任何解决方法来处理这些重复的消息吗?
来自官方文档:
How uninstalled client app unregistration works
A client app can be automatically unregistered after it is
uninstalled. However, this process does not happen immediately. What
happens in this scenario is:
- The end user uninstalls the client app.
- The app server sends a message to GCM connection server.
- The GCM connection server sends the message to the GCM client on the device.
- The GCM client on the device receives the message and detects that the client app has been uninstalled; the detection details depend on the platform on which the client app is running.
- The GCM client on the device informs the GCM connection
server that the client app was uninstalled.
- The GCM connection server
marks the registration token for deletion.
- The app server sends a
message to GCM.
- The GCM returns a NotRegistered error message to the
app server.
- The app server should delete the registration token.
Note
that it might take a while for the registration token to be completely
removed from GCM. Thus it is possible that messages sent during step 7
above get a valid message ID as a response, even though the message
will not be delivered to the client app. Eventually, the registration
token will be removed and the server will get a NotRegistered error,
without any further action being required from the app server.
但是,很明显,您仍然会收到旧注册 ID 的通知,正如用户在其他问题中所述:
针对这个问题,有一个叫做“canonical IDs”的功能:
Canonical IDs
If a bug in the client app triggers multiple registrations for the
same device, it can be hard to reconcile state and the client app
might end up with duplicate messages.
Implementing canonical IDs can help you more easily recover from these
situations. A canonical registration ID is the registration token of
the last registration requested by the client app. This is the ID
that the server should use when sending messages to the device.
If you try to send a message using an old registration token, GCM will
process the request as usual, but it will include the canonical ID in
the registration_id field of the response. Make sure to replace the
registration token stored in your server with this canonical ID, as
eventually the old registration token will stop working.