Google云消息传递(GCM)和registration_id到期,我将如何知道? [英] Google Cloud Messaging (GCM) and registration_id expiry, how will I know?

查看:147
本文介绍了Google云消息传递(GCM)和registration_id到期,我将如何知道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经整理了一个框架客户端/服务器GCM应用程序,它可以从我的粗糙和准备好的服务器接收和显示Android手机上的消息。我说'骨架'和'粗糙',因为我的应用程序的registration_id是硬编码到服务器端的,因为我在启动示例Android应用程序并在GCM服务器上注册应用程序/设备组合时在调试器中捕获的值。我开始着手为双方提供读/写机制,以便在我想到时访问服务器上的简单数据库以存储reg_id /设备组合:



我知道这个reg_id可以随时过期,因为Google说可以。从文档中可以清楚地看到如何检查该ID是否仍然可以,如果这是一个很大的IF,我会再次启动应用程序。但是,为什么我或其他用户再次启动它?在清单中注册的GCMIntentService坐在那里根据需要拾取消息,并且不需要由于任何原因再次运行应用程序。所以当id到期时,我会停止收到消息 - 服务器可能从返回码中知道,但不能通知我的应用程序 - 除非我再次运行应用程序并通过它的 onCreate()和大概 registerClient()方法。

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

根据 GCM


现有的注册ID可能在许多
方案中停止有效,包括: b
$ b


  • 如果应用程序手动取消注册通过发布com.google.android.c2dm.intent.UNREGISTER意图。

  • 如果应用程序自动注销,如果用户卸载应用程序,可能会发生这种情况(但不能保证)。如果注册ID过期。 Google可能决定刷新注册ID。

  • 如果应用程序已更新,但新版本没有配置接收com.google.android.c2dm.intent.RECEIVE意图的广播接收器。

请注意,与注册相关的响应会触发 com.google.android .c2dm.intent.REGISTRATION 意图。所以你应该注意从GCM发送的注册信息。



更新



GCM的文档说:


如果注册ID过期。 Google可能会决定刷新
注册ID。对于所有这些情况,您应该从第三方服务器删除
注册ID,并停止使用它发送
消息。错误代码为NotRegistered时会发生。


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


I've put together a skeleton client/server GCM app that accepts and displays messages on my Android phone from my rough and ready server. I say 'skeleton 'and 'rough and ready' because my app's registration_id is hard coded into the server side as the value I captured in the debugger when I started the sample Android app and registered the app/device combination on the GCM server. I was about to start working on providing a read/write mechanism for the two sides to access a simple database on the server to store the reg_id/device combinations when I got to thinking:

I know this reg_id can expire at any time, because Google say it can. It's clear from the docs how to check that the id is still OK IF, and it's a big IF, I start the app again. However, why should I, or any other user, start it again? The GCMIntentService, registered in the manifest is sitting there picking up messages as required and there's no need to run the app again for any reason. So when the id expires, I'll stop getting messages - the server might know from the return code, but won't be able to inform my app - no recovery unless I run the app again and go through its onCreate() and presumably registerClient() methods.

So what happens when the id expires? I don't see that any of the on.. methods in the IntentService will execute. Will Google themselves send a normal message saying it's about to expire? Do I need to write an alarm task to run daily and check the id?

解决方案

According to 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.

Consider that registration related responses will fire"com.google.android.c2dm.intent.REGISTRATION" intent. So you should take care of Registration messages sent from GCM.

Updated

GCM doc says:

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.

Considering the above fact as the truth, then on the your server you should handle expired IDs and set a flag on them in your db, the next time your android client check your server(finally you must do a periodical check) or you call isRegisteredOnServer on GCMRegistrar class and got a false result, you should fire REGISTRATION intent in order to get a new ID.

这篇关于Google云消息传递(GCM)和registration_id到期,我将如何知道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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