Android 使用 Canonical ID GCM

标签 android google-cloud-messaging

正如我在这里提到的 Android GCM messages repeated 问题是我多次安装和重新安装该应用程序,因此它有很多regId。我发现很多帖子建议使用 Canonical ID。我读了很多帖子,但我真的不知道如何应用它。这是我获取 regId 的代码:

   public String getRegId(){
            int noOfAttemptsAllowed = 3;   // Number of Retries allowed
              int noOfAttempts = 0;          // Number of tries done
              boolean stopFetching = false;     // Flag to denote if it has to be retried or not
              String regId = "";             


              while (!stopFetching) 
              {
                   noOfAttempts ++;
                   try {
                       regId = gcm.register(PROJECT_NUMBER);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                   try 
                   {

                      Thread.sleep(4000);   // Set this timing based on trial.

                      try {
                           regId = gcm.register(PROJECT_NUMBER);
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
               } catch (InterruptedException e) {
                e.printStackTrace();
               }
                  /* try
                   {
                        // Get the registration ID
                        regId = gcm.register(PROJECT_NUMBER);
                   } catch (Exception e) {}*/


                   if (!regId.equals("") || noOfAttempts > noOfAttemptsAllowed)
                   {
                        // If registration ID obtained or No Of tries exceeded, stop fetching
                        stopFetching = true;
                    }
                    if (!regId.equals(""))
                    {
                        // If registration ID Obtained, save to shared preferences
                           SharedPrefrencesMethods.savePreferences(activity, "regid", regid);
                    }


               }
              return regId;
        }

最佳答案

是的。您可以使用规范 ID 来帮助您更轻松地从错误中恢复。它是客户端应用程序请求的最后一次注册的注册 token 。这是服务器向设备发送消息时应使用的 ID。

If you try to send a message using an old registration token, GCM will process the request as usual, but it will include the canonical ID in the registration_id field of the response. Make sure to replace the registration token stored in your server with this canonical ID, as eventually the old registration token will stop working.

基于此blog ,GCM 服务按照发送通知的顺序返回规范 ID。以下是响应示例:

{
    "multicast_id": 7036866281258904189,
    "success": 1,
    "failure": 0,
    "canonical_ids": 1,
    "results": [
        {
            "registration_id": "CANNONICAL_REGISTRATION_ID",
            "message_id": "0:1415529915241995%64ac3713f9fd7ecd"
        }
    ]
}

canonical id = 0 表示您的推送服务器使用的注册 id 是可以的,不应该被 canonical id 替换,即通常 GCM 服务器会响应 canonical_id = 0。 在示例响应中,有一个规范 ID,这意味着您的服务器必须将现有的注册 ID 替换为您在响应中看到的新值。如果用户重新安装您的客户端应用程序,但您的推送服务器不知道这一点,并且 GCM 服务器将在响应中传递新的注册 ID,那么这种情况很容易重现。

检查有关 Canonical ID 的这些相关 SO 问题:

关于Android 使用 Canonical ID GCM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36838237/

相关文章:

android - relativelayout 和 View 与layout_toLeftOf 属性在DialogFragment 中不起作用

android - Android 模拟器上没有剩余空间

java - Dagger 2 麻烦@Inject-ing FirebaseMessagingService

android - 在GCM中,onError(Context context,String errorId)中的错误id是什么

android - 如何保存android配置布局

android - 如何将文件压缩到android中的zip文件夹中?

android - 如何将随机客户端相互配对?

android - 在不启动 Activity 的情况下将 Intent 从 GcmIntentService 发送到当前 Activity

android - 手动注册 GCM 设备

android - 使用 Google 应用引擎的 GCM 服务器