【问题标题】:Android using Canonical ID GCMAndroid 使用 Canonical ID GCM
【发布时间】:2016-04-25 10:40:37
【问题描述】:

正如我在这里提到的 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;
        }

【问题讨论】:

    标签: android google-cloud-messaging


    【解决方案1】:

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

    如果您尝试使用旧的注册令牌发送消息,GCM 将照常处理请求,但它会在响应的 registration_id field 中包含规范 ID。请务必使用此规范 ID 替换存储在您的服务器中的注册令牌,因为最终旧的注册令牌将停止工作。

    基于此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。

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

    【讨论】:

    • 好吧,谢谢你,我现在明白了,但我有一个小问题我不知道在哪里接收服务器响应我的意思是我有 onHandleIntent 意图方法我试图只能获取消息本身?
    • 您好,我设法从服务器获取响应并获取规范 ID,但我发现许多规范 id 我应该使用哪个这是 json 响应的一部分 {"success":17,"failure":15 ,"canonical_ids":16}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    相关资源
    最近更新 更多