Android C2DM:向同一设备和应用程序重复消息
我想知道是否有人在使用 Google C2DM 时遇到过这个问题?这是我面临的场景:
- 用户安装应用程序并注册 与C2DM服务器进行注册 钥匙。
- 用户卸载应用程序。
- 用户重新安装应用程序(并且 向 C2DM 服务器注册新的 注册密钥)。
现在,我从服务器向用户的手机发送消息,他们收到重复的消息。
任何人都可以深入了解这是否是预期的行为或者我如何解决它? 谢谢,
I'm wondering if anyone has faced this issue with Google C2DM? This is the scenario I am faced with:
- User installs the app and registers
with C2DM server for a registration
key. - User uninstalls the app.
- User reinstalls the app (and
registers with C2DM server for new
registration key).
Now I send message from my server to the user's phone and they get a duplicate message.
Could anyone shed any insight into wether this is expected behaviour or how I can fix it?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不确定这是否是最好的方法,但在 <一个 href="https://groups.google.com/forum/#!forum/android-c2dm">android-c2dm 小组,海报提供了一种技术:
Not sure if this is the best approach, but there's a relevant thread over at the android-c2dm group, where the poster offers one technique:
这种情况应该只发生在重新安装应用程序后的第一个推送通知中。
在检测已卸载的应用程序时,Google C2DM 服务以被动模式工作。
卸载应用程序后的首次推送通知(无需从 C2DM 注销!!!)不会返回任何错误响应。但是,第二个推送通知将返回“无效注册”或“未注册”错误代码,您可以在其中意识到应用程序已被卸载。
原因是C2DM服务器立即返回响应代码,然后才尝试推送客户端。当客户端响应应用程序已卸载时,该应用程序将从 C2DM 服务器中删除。下一次推送尝试将立即返回错误代码。
This should only happen for the first push notification after re-installing your application.
Google C2DM service is working in passive mode when it comes to detecting uninstalled applications.
First push notification after uninstalling your application (without unregistering from C2DM!!!) will NOT return any error in response. However, the second push notification will return an "invalid registration" or "not registered" error codes where you can realize the application was uninstalled.
The reason is that C2DM servers return the response code immediately and only then tries to push the client. When client respond that an application was uninstalled, it is deleted from C2DM servers. Next push attempt will return an error code immediately.
另一种解决方案可能是为您的服务器提供设备的唯一标识符。在这种情况下,当设备在重新安装后尝试注册时,您只需更新该 UUID 的注册 ID 即可。
Another solution could be to provide your server with a unique identifier for the device. In that case you can just update the registrationID for that UUID when the device tries to register after re-installation.
是的,我遇到了同样的问题,在我看来,这是 Android C2DM 实现中的一个重大疏忽。 iOS 可以更好地处理这一问题,因为应用程序只能接收一个且仅一个设备令牌(相当于 c2dm 注册 ID)的通知。
我使用的解决方法是将注册 ID 的最后 10 个字符作为 c2dm 负载的一部分发送然后在我的 onMessage 方法中进行以下检查:
Yup, I've run into the same issue and in my opinion it's a big oversight in the Android C2DM implementation. iOS handles this much better in that an app can only ever receive notifications for one and only one device token (equivalent of the c2dm registration id)
The workaround I use is to send the last 10 characters of the registration id as part of the c2dm payload and then in my onMessage method I do the following check:
@Zamel 和 @johan 的答案都很好,需要结合起来。如果将这两种解决方案结合起来,您将最大限度地减少服务器的数据库。
因此,最好的解决方案是:
在将推送令牌发送到服务器时发送设备 ID
在为现有设备 ID 发送时更新推送令牌
当推送令牌被识别为“无效注册”时”或“未注册”您可以使其无效(将其标记为空)、删除数据库中的行或实现过期功能。这取决于您的需求
Both @Zamel and @johan answers are good and need to be combined. If you combine both solutions than you will minimize your server's database.
So the best solution will be to:
Send device id when sending the push token to the server
Update push token when is sent for existing device id
When push token is recognized as "invalid registration" or "not registered" you can invalidate it(mark it as null), delete the row in the database or implement expiration functionality. It depends on your needs