13

我有一个关于 Google Cloud Messaging 的问题...

我将 GCM 发送给 Google 以获取 3 个注册 ID,然后 Google 回复说 2 个注册 ID 已成功发送,一个未成功发送,因为注册 ID 错误!

但它并没有告诉我哪个注册 ID 尚未发送...

现在这是我的问题:如何解析 Google GCM 响应以获取尚未发送的注册 ID?谷歌是否有 API 或其他东西,以便我可以给它“multicat_id”并告诉我哪个注册 ID 有问题?

任何帮助将不胜感激,我很困惑:)

4

3 回答 3

15

它基于顺序:

假设你发送这个:

{ "collapse_key": "score_update",
  "time_to_live": 108,
  "delay_while_idle": true,
  "data": {
    "score": "4x8",
    "time": "15:16.2342"
  },
  "registration_ids":["4", "8", "15", "16", "23", "42"]
}

并从谷歌得到这个回应:

{ "multicast_id": 216,
  "success": 3,
  "failure": 3,
  "canonical_ids": 1,
  "results": [
    { "message_id": "1:0408" },
    { "error": "Unavailable" },
    { "error": "InvalidRegistration" },
    { "message_id": "1:1516" },
    { "message_id": "1:2342", "registration_id": "32" },
    { "error": "NotRegistered"}
  ]
}

这意味着您发送的第 3 个注册 ID(15)无效,第 6 个(42)未注册。两者都应该从您的数据库中删除。

于 2013-06-04T15:22:10.470 回答
9

我是这样做的:

$gcm_result = $gcm->send_notification($registration_ids, $message);
$jsonArray = json_decode($gcm_result);

if(!empty($jsonArray->results)){

    $remove_ids = array();

    for($i=0; $i<count($jsonArray->results);$i++){
        if(isset($jsonArray->results[$i]->error)){
            if($jsonArray->results[$i]->error == "NotRegistered"){
                $remove_ids[$i] = "'".$registration_ids[$i]."'";
            }
        }
    }

    if(!empty($remove_ids)){

        $remove_ids_string = implode(', ',$remove_ids);
        include_once SCRIPTS.'gcm_server_php/db_functions.php';
        $dbf = new DB_Functions();
        $res = $dbf->deleteUsers($remove_ids_string);
        echo count($remove_ids)." users have unregistered from notifications since the last one was sent out.";

    }
}
于 2013-07-03T00:41:18.547 回答
2

根据我在这里阅读的内容:

http://developer.android.com/google/gcm/http.html#error_codes http://developer.android.com/google/gcm/server-ref.html#error-codes

我开发如下(尚未测试):

$result = $gcm->send_notification($registration_ids, $message);
$jsonArray = json_decode($result);

if($jsonArray->canonical_ids != 0 || $jsonArray->failure != 0){
    if(!empty($jsonArray->results))
    {
        for($i = 0 ; $i<count($jsonArray->results) ; $i++){
            $result = $jsonArray->results[$i];
            if(isset($result->message_id) && isset($result->registration_id))
            {
                // You should replace the original ID with the new value (canonical ID) in your server database
            }
            else
            {
                if(isset($result->error))
                {
                    switch ($result->error)
                    {
                        case "NotRegistered":
                        case "InvalidRegistration":
                            // You should remove the registration ID from your server database
                            break;
                        case "Unavailable":
                        case "InternalServerError":
                            // You could retry to send it late in another request.
                            break;
                        case "MissingRegistration":
                            // Check that the request contains a registration ID
                            break;
                        case "InvalidPackageName":
                            // Make sure the message was addressed to a registration ID whose package name matches the value passed in the request.
                            break;
                        case "MismatchSenderId":
                            // Invalid SENDER_ID
                            break;
                        case "MessageTooBig":
                            // Check that the total size of the payload data included in a message does not exceed 4096 bytes
                            break;
                        case "InvalidDataKey":
                            // Check that the payload data does not contain a key that is used internally by GCM.
                            break;
                        case "InvalidTtl":
                            // Check that the value used in time_to_live is an integer representing a duration in seconds between 0 and 2,419,200.
                            break;
                        case "DeviceMessageRateExceed":
                            // Reduce the number of messages sent to this device
                            break;

                    }
                }
            }
        }
    }
}
于 2015-04-01T10:16:22.307 回答