Android GCM 注册 ID 会过期吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11590482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:10:24  来源:igfitidea点击:

Do GCM registration id's expire?

androidandroid-notificationsgoogle-cloud-messaging

提问by Mohamed Hafez

I know that C2DM registrations expire, and you are supposed to periodically refresh the registration ID. Is this the case with GCM? by looking at the following code on the Android GCM guide (shown below), it seems like you only do it once and don't need to refresh, but I dont see that explicitly written anywhere, so I just wanted to check.

我知道 C2DM 注册会过期,您应该定期刷新注册 ID。这是 GCM 的情况吗?通过查看Android GCM指南(如下所示)上的以下代码,您似乎只执行一次并且不需要刷新,但是我没有看到在任何地方明确写入,所以我只是想检查一下。

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

采纳答案by Mohamed Hafez

EDIT: THIS ANSWER IS WAY OUT OF DATE, I HAVE NO IDEA WHAT THE CURRENT BEHAVIOR IS

编辑:这个答案已经过时了,我不知道当前的行为是什么



I found the answer myself. You don't explicitly need to re-register all the time, just once according to the example in the docs.

我自己找到了答案。根据文档中的示例,您不需要一直明确地重新注册,只需重新注册一次

Also, unlike previous versions of GCM and C2DM, Google itself does notrefresh the registration itself now: once you have the registration id from the initial registration you are good to go, exceptfor one case: you do still need to re-register when the user upgrades to a new version (this case is also handled in the example in the link above):

此外,与以前版本的 GCM 和 C2DM 不同,Google 本身现在不会刷新注册本身:一旦您获得了初始注册的注册 ID,您就可以开始使用了,除了一种情况:您仍然需要在以下情况下重新注册用户升级到新版本(这种情况也在上面链接中的示例中处理):

When an application is updated, it should invalidate its existing registration ID, as it is not guaranteed to work with the new version. Because there is no lifecycle method called when the application is updated, the best way to achieve this validation is by storing the current application version when a registration ID is stored.

当应用程序更新时,它应该使其现有的注册 ID 失效,因为它不能保证与新版本一起使用。因为在应用程序更新时没有调用生命周期方法,所以实现此验证的最佳方法是在存储注册 ID 时存储当前应用程序版本。

回答by I?igo

I think that it is refreshed eventually, yes. From the official docs:

我认为它最终会刷新,是的。来自官方文档:

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

在许多情况下,现有注册 ID 可能不再有效,包括: 如果应用程序通过发出 com.google.android.c2dm.intent.UNREGISTER 意图手动取消注册。如果应用程序自动注销,如果用户卸载应用程序,这可能会发生(但不能保证)。如果注册 ID 过期。Google 可能会决定刷新注册 ID。对于所有这些情况,您应该从第 3 方服务器中删除此注册 ID,并停止使用它来发送消息。当错误代码为 NotRegistered 时发生。

This could happen in a request to GCM from your 3rd-party server, which returns a json response with the error Unregistered Device.

这可能发生在从您的第 3 方服务器向 GCM 发出的请求中,该请求返回带有错误Unregistered Device的 json 响应。

Once this happen, it'll be up to you to refresh the corresponding id's.

一旦发生这种情况,由您来刷新相应的 ID。

http://developer.android.com/guide/google/gcm/gcm.html

http://developer.android.com/guide/google/gcm/gcm.html