Android GCM 和多个令牌 [英] Android GCM and multiple tokens

查看:18
本文介绍了Android GCM 和多个令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 GoogleCloudMessaging.getInstance(context) 在 GCM 中注册;并将收到的令牌保存在设备上.然后将其发送到服务器并与用户帐户相关联.如果我卸载我的应用程序而不注销并再次安装并使用另一个用户登录,我会收到新令牌并将其发送到服务器.当推送发送给第一个用户时,我会在使用第二个用户登录时看到它们.

I register in GCM with GoogleCloudMessaging.getInstance(context); and save received token on device. Then send it to server and it's associated with user account. If I uninstall my app without logging out and install again and log in with another user, I receive new token and send it to server. And when pushes being send to first user I see them when I logged in with second user.

为什么 GCM 会向我发送不同的令牌,我该如何处理?

Why does GCM sends me different tokens and how can I handle this?

推荐答案

欢迎来到来自 Google Cloud Messaging 的重复消息的奇妙世界.发生这种情况时,GCM 引擎会启用 Canonical IDs 来解决它.这可能是因为您为同一设备注册了多个 ID,或者因为 GCM 服务器在卸载应用程序时没有收到 unregister() 调用.使用规范 ID 会将您的 ID 设置为您最后进行的注册.

Welcome to the marvelous world of duplicate messages from Google Cloud Messaging. When this happens, the GCM engine enables the Canonical IDs to solve it. This might happen because you registered with several IDs for the same device, or because the GCM server didn't get the unregister() call when the app was uninstalled. Using canonical IDs will set your ID to be the last registration you've made.

根据关于此的 GCM 参考:

规范 ID

在服务器端,只要应用程序运行良好,一切都应该正常运行.但是,如果应用程序中的错误触发了同一设备的多次注册,则可能很难协调状态,最终可能会收到重复的消息.

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 提供了一种称为规范注册 ID"的工具.轻松地从这些情况中恢复过来.规范的注册 ID 被定义为您的应用程序请求的最后一个注册的 ID.这是服务器在向设备发送消息时应使用的 ID.

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.

如果稍后您尝试使用不同的注册 ID 发送消息,GCM 将照常处理请求,但它会在响应的 registration_id 字段中包含规范的注册 ID.请务必使用此规范 ID 替换存储在服务器中的注册 ID,因为最终您使用的 ID 将停止工作.

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.

更多信息此处.

还有一个关于如何进行的实际案例,它可能会有所帮助:

Also there is a practical case on how to procceed, it might be helpful:

这篇关于Android GCM 和多个令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆