处理Android上的Google Cloud Messaging中的注册ID更改

处理Android上的Google Cloud Messaging中的注册ID更改

在Google Cloud Messaging的文档中,它指出:

Android应用程序应该存储此ID以供以后使用(例如,如果已经注册,则检查onCreate())。请注意,Google可能会定期刷新注册ID,因此您应该设计Android应用程序,并了解可能会多次调用com.google.android.c2dm.intent.REGISTRATION意图。您的Android应用程序需要能够做出相应的响应。

我使用以下代码注册我的设备:

GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);String regID = gcm.register(senderID);

GoogleCloudMessaging类封装了注册过程。那么我想如何处理com.google.android.c2dm.intent.REGISTRATION,因为处理是由GoogleCloudMessaging类在内部完成的?


慕莱坞森
浏览 725回答 3
3回答

呼啦一阵风

在通过网络(包括SO)清除大量误导性答案之后,我找到一个完整答案的唯一地方就是Eran的回答和这里所说的:虽然自动注册刷新可能会或可能永远不会发生,但谷歌通过解析成功响应来描述一个simiple算法来处理canocical_ids:If the value of failure and canonical_ids is 0, it's not necessary to parse the remainder of the response. Otherwise, we recommend that you iterate through the results field and do the following for each object in that list: If message_id is set, check for registration_id: If registration_id is set, replace the original ID with the new value (canonical ID) in your server database. Note that the original ID is not part of the result, so you need to obtain it from the list of code>registration_ids passed in the request (using the same index). Otherwise, get the value of error: If it is Unavailable, you could retry to send it in another request. If it is NotRegistered, you should remove the registration ID from your server database because the application was uninstalled from the device or it does not have a broadcast receiver configured to receive com.google.android.c2dm.intent.RECEIVE intents. Otherwise, there is something wrong in the registration ID passed in the request; it is probably a non-recoverable error that will also require removing the registration from the server database. See Interpreting an error response for all possible error values.从上述链接。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android