我目前正在尝试使用GCM向用户发送通知,目前我仍在研究如何将其最大化。现在我只使用文档此处提供的示例项目,并使用gcm-client示例来处理它。
现在使用来自Git的项目我尝试使用应用程序创建的注册ID推送消息,并且它已成功传递消息。
现在问题是我卸载应用程序后。重新安装后,它会生成一个新的注册ID,我将它与前一个一起存储在服务器上,不同的是我不能标记以前的注册ID以便不会收到任何进一步的消息,因为当用户没有互联网时卸载可能发生连接。之后,我会在重新安装应用程序之后,将消息发送到两个注册ID(它是卸载应用程序之前的ID)和ID。发生什么事是我收到了两条推送消息,尽管我预料它只会得到一条消息,因为应用程序已经更改了注册ID。
我预计应用程序可能会收到更多重复的应用程序,如果有的话,我也更新了应用程序,因为在文档上说注册ID可能会更新。
我可以做任何解决方法来处理这些重复的消息?
解决方案如何卸载客户端应用程序取消注册的工作原理
在卸载后,客户端应用程序可以自动取消注册。但是,这个过程不会立即发生。在这种情况下发生的是: $ b
请注意,注册令牌可能需要一段时间才能完全从GCM中删除。因此,即使消息不会传递到客户端应用程序,也可能在上面的步骤7 中发送的消息获得有效的消息ID。最终,注册令牌将被删除,服务器将收到NotRegistered错误,而不需要从应用服务器进一步采取任何行动。
但是,显然您仍然会收到旧注册ID的通知,因为用户在其他问题中声明:
对于这个问题,有一个名为canonical IDs的功能:
Canonical IDs
客户端应用程序触发相同设备的多个注册,它可能很难协调状态,客户端应用程序可能以重复消息结束。
实现规范ID可以帮助您更轻松地从这些情况中恢复。 规范注册ID是客户端应用程序请求的最后一次注册的注册令牌。这是服务器在将消息发送到设备时应使用的ID 。
如果您尝试使用旧注册令牌,GCM将像往常一样处理请求,但它将在响应的registration_id字段中包含中的规范ID。 确保使用此规范ID替换服务器中存储的注册令牌,因为最终会使旧注册令牌停止工作。
I'm currently trying to use GCM to send notification to user and currently I'm still studying on how I can maximize it. For now I just use the sample project provided on the documentation here and I use the gcm-client sample to work on it.
Now using this project from Git I tried to push a message using the registration ID created by the app and yes it successfully delivers the message.
Now the problem is that after I uninstalled the application. After I reinstall it it will generate a new registration ID wherein I store it on a server together with the previous one except that I can't tag the previous registration ID to not receive any further message since the uninstall might happen when user has no internet connection. After that I send a message to two registration ID's which is the ID before uninstalling the app and the ID after reinstalling the application. What happen is that I receive two push messages eventhough I expected it to only get one since the app already changes the registration ID.
I expect that the app might receive twoor more duplicate apps if ever I also updated the app since as said on documentation the registration ID might change on update.
Any workaround I can do to handle this duplicate messages?
解决方案From the official documentation:
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:
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.
However, it can apparently happen that you still get the notification for the old registration ID, as users state in other questions:
- Android GCM and multiple tokens
- Unregistering and re-registering for GCM messages causes two regId's to be valid. Is this as intended?
- Do old GCM tokens live on even after an uninstall?
For this problem, there is a functionality called "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.
更多推荐
应用程序在重新安装后使用GCM接收到重复通知
发布评论