如何检查设备是否已通过GCM注册,因为官方教程声明“regID不能无限期地持续使用” [英] How to check if the device if registered with GCM because official tutorial states "regID is not guaranteed to last indefinitely"

查看:252
本文介绍了如何检查设备是否已通过GCM注册,因为官方教程声明“regID不能无限期地持续使用”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问过几次了,但是我还没有找到满意的答案,因为Google的GCM教程中的一部分文本让我感到困惑:


给定的regID并不能保证无限期持续,因此您的应用程序应始终执行的第一件事是检查以确保其具有有效的regID(如上面的代码片段所示)


我在设备表中保存返回的ID和应用程序版本,每次在启动程序活动应用程序中检查ID,并在ID不存在的情况下注册设备。

但是,它们意味着regID不能无限期地持续下去。设备如何知道它是否在GCM服务器上丢失了它的regID,如果它仅在Shared preferences或Table中进行本地检查? 阅读代码,Google似乎只希望我们重新注册应用程序更新。

  private String getRegistrationId(Context context){
final SharedPreferences prefs = getGCMPreferences(context);
字符串registrationId = prefs.getString(PROPERTY_REG_ID,);
if(registrationId.isEmpty()){
Log.i(TAG,Registration not found。);
return;
}
//检查应用是否已更新;如果是这样,它必须清除注册ID
//因为现有的REGID不能保证与新的
// app版本一起工作。
int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION,Integer.MIN_VALUE);
int currentVersion = getAppVersion(context);
if(registeredVersion!= currentVersion){
Log.i(TAG,App version changed。);
return;
}
返回registrationId;
}

GCMRegistrar.getRegistrationId 也会做同样的事情。现在已弃用

  public static String getRegistrationId(Context context){
final SharedPreferences prefs = getGCMPreferences(context);
字符串registrationId = prefs.getString(PROPERTY_REG_ID,);
//检查应用是否已更新;如果是这样,它必须清除注册ID为
//如果GCM发送消息,则避免竞争条件
int oldVersion = prefs.getInt(PROPERTY_APP_VERSION,Integer.MIN_VALUE);
int newVersion = getAppVersion(context);
if(oldVersion!= Integer.MIN_VALUE&&oldVersion!= newVersion){
Log.v(TAG,应用版本从+ oldVersion +更改为+
newVersion +;重置注册ID);
clearRegistrationId(context);
registrationId =;
}
返回registrationId;
}

编辑

阅读文档和代码(至少)我无法找到任何其他方式让客户知道它是否需要重新注册。但是,尝试将通知发送到过期或重复的注册ID时,您可能会收到一些服务器错误响应。如果注册ID已过期,应用服务器可以要求客户重新注册,并且如果它是重复的应用服务器应该使用Google返回的 canonical_id 来替换现有的注册ID。

请阅读链接了解更多关于如何解释这些错误消息的细节。



希望这有助于。


I know this question has been asked few times already, but I haven't found satisfying answer because one part of text in Google's tutorial for GCM bugs me:

A given regID is not guaranteed to last indefinitely, so the first thing your app should always do is check to make sure it has a valid regID (as shown in the code snippets above)

I am saving returned ID and application version in table on device and every time in launcher activity application check for ID and registers device if ID does not exists.

But, how do they mean "regID is not guaranteed to last indefinitely". How can device know if it has lost its regID on GCM server if it checks for it locally only in Shared preferences or in Table?

解决方案

Going through code, It seems Google only wants us to re-register on App updates.

private String getRegistrationId(Context context) {
   final SharedPreferences prefs = getGCMPreferences(context);
   String registrationId = prefs.getString(PROPERTY_REG_ID, "");
   if (registrationId.isEmpty()) {
      Log.i(TAG, "Registration not found.");
      return "";
   }
   // Check if app was updated; if so, it must clear the registration ID
   // since the existing regID is not guaranteed to work with the new
   // app version.
   int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
   int currentVersion = getAppVersion(context);
   if (registeredVersion != currentVersion) {
     Log.i(TAG, "App version changed.");
     return "";
   }
   return registrationId;
}

GCMRegistrar.getRegistrationId also does the same thing. Which is now deprecated.

public static String getRegistrationId(Context context) {
    final SharedPreferences prefs = getGCMPreferences(context);
    String registrationId = prefs.getString(PROPERTY_REG_ID, "");
    // check if app was updated; if so, it must clear registration id to
    // avoid a race condition if GCM sends a message
    int oldVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
    int newVersion = getAppVersion(context);
    if (oldVersion != Integer.MIN_VALUE && oldVersion != newVersion) {
        Log.v(TAG, "App version changed from " + oldVersion + " to " +
                newVersion + "; resetting registration id");
        clearRegistrationId(context);
        registrationId = "";
    }
    return registrationId;
}

EDIT

Going through the documentation and code (at-least) I could not find any other way for client to know if it needs to re-register. However, you can get some server error responses when trying to send notification to expired or a duplicated registration ID. If the registration ID has expired the app server can ask the client to re-register and if it is duplicate app sever should replace the existing reg id with canonical_id which Google returns.

Please read this link for more details on how interpret those error messages.

Hope this helps.

这篇关于如何检查设备是否已通过GCM注册,因为官方教程声明“regID不能无限期地持续使用”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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