明确重新注册设备GCM [英] Explicitly re-register device to GCM

查看:217
本文介绍了明确重新注册设备GCM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个错误的事实,每次我登录到我的Andr​​oid应用程序的时候,我做的第一件事就是注册GCM。
我知道这是没有必要做,但为了检查注册ID被刷新到一个新的或它仍然是相同的,我打算重新注册。

Is there something wrong in the fact that every time I log in to my android application, the first thing I do is to register to GCM. I know it's not necessary to do this, but in order to check if the registration id is refreshed to a new one or it s still the same, I plan to re-register.

final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
  GCMRegistrar.register(this, SENDER_ID);
} else {
  Log.v(TAG, "Already registered");
}

我也想知道,如果通过重新注册到GCM我莫名其妙地迫使registrationID到期。

I would also like to know if by re-registering to GCM I am somehow forcing the registrationID to expire.

感谢您!

推荐答案

第1部分:

为了检查是否登记ID是刷新到一个新的,或者它仍然是相同的,我打算重新注册。

In order to check if the registration id is refreshed to a new one or it is still the same, I plan to re-register.

现在,如果你看到标题下的第二个点在结构概述启用GCM 页面,它说:

Now, if you see the second point under the heading Enable GCM on Architectural Overview page, it says:

请注意,谷歌可能会定期刷新注册ID,所以你应该设计自己的理解是,com.google.android.c2dm.intent.REGISTRATION意图可能被多次调用Android应用程序。你的Andr​​oid应用程序需要能够作出相应的反应。

所以,谷歌会自动发送你这个​​广播,当它更新的ID。你可能不一定发送检查ID的emptynes​​s。当谷歌改变了ID,那么也不会是空的,对不对?所以,如果你要处理的ID的更新/清爽,那么你可以做到以下几点:

So, Google automatically sends you this broadcast, when it renews the ID. You might not necessarily send check the emptyness of the ID. When Google changes the ID, then also it won't be empty, right? So if you want to handle the renewal/refreshing of the ID then you may do the following:

您应该有一个广播监听器这可以处理 com.google.android.c2dm.intent.REGISTRATION 意图,其中谷歌发送到应用程序时,它刷新的注册ID。广播接收机将有的onReceive 法的意图。从意向即可获得捆绑使用,你可以提取谷歌新的注册ID。您可以保存并将其发送给第三部分的服务器来替换你的previous注册ID为该用户。

You should have a Broadcast Listener which could handle com.google.android.c2dm.intent.REGISTRATION intent, which Google send to the app when it has to refresh the registration ID. The broadcast receiver will have the onReceive method with an Intent. From the intent you can get the Bundle using which you can extract the new registration ID from Google. You can save that and send it to the 3rd part server to replace your previous registered ID for that user.

另外你可能会看到回答这个问题<一href=\"http://stackoverflow.com/questions/17335572/in-googlecloudmessaging-api-how-to-handle-the-renewal-or-expiration-of-registra/\">In Google云端通讯API,如何处理好注册ID的更新或过期的?。

Also you may see this answer on the question In GoogleCloudMessaging API, how to handle the renewal or expiration of registration ID?.

第2部分:
回答你的问题的第二部分:

Part 2: Answering your second part of question:

您可能需要阅读取消注册在这里。该文档称如果您注销,然后重新注册,GCM可以返还相同ID或不同的ID,也不能保证任何一种方式。

You may want to read Unregistration here. The docs says If you unregister and then re-register, GCM may return the same ID or a different ID—there's no guarantee either way.

我认为,每当一个ID将是即将到期/更新,谷歌将向您发送新的ID。然后从谷歌的响应将包含一个规范的注册ID(这是新注册ID)。这种反应表明您的服务器应该删除旧的注册ID,并只使用新的。 (来源:答案在<一个href=\"http://stackoverflow.com/questions/16320549/unregistering-and-re-registering-for-gcm-messages-causes-two-regids-to-be-valid\">Unregistering并重新注册为GCM的消息会导致出现两次REGID的是有效的。这是由@Eran

I think that whenever an ID will be about to expire/renew, Google will send you the new ID. Then the response from Google will contain a Canonical Registration ID (which is the new registration ID). This response indicates that your server should delete the old registration ID and use only the new one. ( Source : Answer at Unregistering and re-registering for GCM messages causes two regId's to be valid. Is this as intended? question by @Eran)

希望这有助于您了解如何处理它。

Hope this helps you understand how to handle it.

这篇关于明确重新注册设备GCM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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