android - 应用程序在重新安装后使用 GCM 收到重复通知

标签 android google-cloud-messaging

<分区>

我目前正在尝试使用 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:

  1. The end user uninstalls the client app.
  2. The app server sends a message to GCM connection server.
  3. The GCM connection server sends the message to the GCM client on the device.
  4. 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.
  5. The GCM client on the device informs the GCM connection server that the client app was uninstalled.
  6. The GCM connection server marks the registration token for deletion.
  7. The app server sends a message to GCM.
  8. The GCM returns a NotRegistered error message to the app server.
  9. 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.

关于android - 应用程序在重新安装后使用 GCM 收到重复通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27315169/

相关文章:

java - 使用可变参数和泛型时出现 ClassCastException

ios - iOS自定义声音通知位置

android - 推送通知 GCM DELPHI XE8

php - 尝试将数据发送到 GCM 时出现 MissingRegistraton 错误

android - GCM 云连接服务器 (CCS) 是否存储消息

android - GcmBroadcastReceiver onReceive 方法未在 Android 4.0.3 上触发

java - 编译器是否优化了同一语句中对同一方法的多次调用?

java - 带有按钮的 Android Eclipse 弹出消息

c# - 如何在android中创建一个选项菜单?

android - Bluemix 中 Android 上的 MobileFirst 出现 token 错误