android - Google Cloud Messaging (GCM) 和 registration_id 过期了,我怎么知道?

标签 android google-cloud-messaging

我组装了一个骨架客户端/服务器 GCM 应用程序,它从我的粗糙且准备好的服务器上接受消息并在我的 Android 手机上显示消息。我说“骨架”和“粗略且准备就绪”是因为我的应用程序的 registration_id 被硬编码到服务器端,作为我在启动示例 Android 应用程序并在 GCM 服务器上注册应用程序/设备组合时在调试器中捕获的值。当我开始思考时,我正要开始为双方提供一种读/写机制来访问服务器上的一个简单数据库来存储 reg_id/device 组合:

我知道这个 reg_id 可以随时过期,因为谷歌说它可以。从文档中可以清楚地看出如何检查 id 是否仍然正常,如果,如果很大,我再次启动应用程序。但是,我或任何其他用户为什么要重新启动它?在 list 中注册的 GCMIntentService 正坐在那里根据需要接收消息,无需出于任何原因再次运行该应用程序。因此,当 ID 过期时,我将停止接收消息 - 服务器可能从返回码中获知,但无法通知我的应用程序 - 除非我再次运行该应用程序并通过其 onCreate( ) 和大概的 registerClient() 方法。

那么id过期了怎么办?我没有看到 IntentService 中的任何 on.. 方法都会执行。谷歌自己会发送一条正常消息说它即将过期吗?是否需要写一个告警任务每天运行并查看id?

最佳答案

根据 GCM :

An existing registration ID may cease to be valid in a number of scenarios, including:

  • If the application manually unregisters by issuing a com.google.android.c2dm.intent.UNREGISTER intent.
  • If the application is automatically unregistered, which can happen (but is not guaranteed) if the user uninstalls the application. If the registration ID expires. Google might decide to refresh registration IDs.
  • If the application is updated but the new version does not have a broadcast receiver configured to receive com.google.android.c2dm.intent.RECEIVE intents.

考虑与注册相关的响应将触发“com.google.android.c2dm.intent.REGISTRATION” Intent 。因此,您应该注意从 GCM 发送的注册消息。

已更新

GCM 文档说:

If the registration ID expires. Google might decide to refresh registration IDs. For all these cases, you should remove this registration ID from the 3rd-party server and stop using it to send messages. Happens when error code is NotRegistered.

考虑到上述事实,那么在你的服务器上你应该处理过期的ID并在你的数据库中为它们设置一个标志,下次你的android客户端检查你的服务器(最后你必须定期检查)或者您在 GCMRegistrar 类上调用 isRegisteredOnServer 并得到一个 false 结果,您应该触发 REGISTRATION Intent 以获得新 ID。

关于android - Google Cloud Messaging (GCM) 和 registration_id 过期了,我怎么知道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12824924/

相关文章:

android - 为什么android中有ListVIew?

java - Android ListView 和 ArrayAdapter/Arraylist

android - 操作栏选项卡选择分隔线颜色

java - 动态元素数量布局问题

android - 在android应用程序中,调用onResume方法时清除状态栏中的小图标

android - 一台服务器向 Android 和 iOS 设备发送推送通知

android - OneSignal-ANDROID-NotificationExtenderService

ios - 在 ios 上使用 GCM 推送通知

java - 如何自定义 GCM Message Builder

javascript - 谷歌云消息 (fake_message_id)