明确地将设备重新注册到GCM

时间:2013-08-23 08:45:16

标签: android google-cloud-messaging

每次登录我的Android应用程序时,是否有问题,我做的第一件事就是注册GCM。 我知道没有必要这样做,但为了检查注册ID是否刷新到新的或者仍然相同,我打算重新注册。

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

我还想知道,如果通过重新注册GCM,我会以某种方式迫使registrationID过期。

谢谢!!!

1 个答案:

答案 0 :(得分:2)

第1部分:

  

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

现在,如果您在Architectural Overview页面上的“启用GCM”标题下看到第二个点,则说明:

Note that 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.

因此,Google会在续订ID时自动向您发送此广播。您可能不一定要检查ID的空白。当Google更改ID时,它也不会为空,对吧?因此,如果您想要处理ID的续订/刷新,那么您可以执行以下操作:

您应该有Broadcast Listener可以处理com.google.android.c2dm.intent.REGISTRATION意图,Google必须刷新注册ID时才会向该应用发送该意图。广播接收器将具有带有Intent的onReceive方法。根据意图,您可以使用Bundle从中提取Google的新注册ID。您可以将其保存并发送到第三方服务器,以替换该用户以前注册的ID。

另外,您可能会在问题this上看到In GoogleCloudMessaging API, how to handle the renewal or expiration of registration ID?回答。

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

您可能需要在此处阅读Unregistration。文档说If you unregister and then re-register, GCM may return the same ID or a different ID—there's no guarantee either way.

我认为只要ID即将到期/续订,Google就会向您发送新ID。然后,Google的回复将包含规范注册ID(这是新的注册ID)。此响应表示您的服务器应删除旧的注册ID并仅使用新的注册ID。 (来源:@Eran在Unregistering and re-registering for GCM messages causes two regId's to be valid. Is this as intended?提问时回答)

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