正如问题所说,如何找出注册 ID何时在GoogleCloudMessaging API中变得无效?我已经阅读了关于类似主题的几个问题的答案:GCM 注册 ID 是否过期?和Google Coud Mesaging (GCM) 和 registration_id 到期,我怎么知道?. 这些问题的问题在于,C2DM
旧 GCM API 的答案是使用GCMRegistrar而不是 GoogleCloudMessaging API。前两种方法已贬值。
我将尝试逐步打破我的困惑/问题:
1)在标题Enable GCM下,第二点说:
Google may periodically refresh the registration ID, so you should design your Android application with the understanding that the com.google.android.c2dm.intent.REGISTRATION intent may be called multiple times. Your Android application needs to be able to respond accordingly.
The registration ID lasts until the Android application explicitly unregisters itself, or until Google refreshes the registration ID for your Android application. Whenever the application receives a com.google.android.c2dm.intent.REGISTRATION intent with a registration_id extra, it should save the ID for future use, pass it to the 3rd-party server to complete the registration, and keep track of whether the server completed the registration. If the server fails to complete the registration, it should try again or unregister from GCM.
2)现在,如果是这种情况,那么我应该在 BroadcastReceiver 中处理意图并再次发送register()请求以获取新的注册 ID。但问题是在同一页面上的标题 ERROR_MAIN_THREAD下,它说:
GCM methods are blocking. You should not run them in the main thread or in broadcast receivers
.
3)我还了解注册 ID 更改时还有其他两种情况(如“保持注册状态同步”标题下的高级主题中所述):应用程序更新和备份和恢复。我已经在打开应用程序时处理它们。
4)在 GCMRegistrar API 中,在GCMBaseIntentService内部,曾经有一个回调 onRegistered()方法,该方法在设备注册时被调用。在这里,我曾经保留注册 ID 并发送到 3rd 方服务器。
但是,现在我应该如何处理注册 ID 的更新或更新,将其保存并发送到 3rd 方服务器?
可能是我读完所有内容感到困惑,或者我遗漏了一些东西。我会非常感谢你的帮助。
更新
即使在Android线程上处理Google Cloud Messaging中的注册ID更改,也没有提到如何处理Google定期刷新ID?