首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用php解析GCM响应以从服务器中删除无效注册id

如何使用php解析GCM响应以从服务器中删除无效注册id
EN

Stack Overflow用户
提问于 2013-06-04 23:01:44
回答 3查看 13.1K关注 0票数 13

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

我发送GCM到谷歌的3个注册ID,然后谷歌回复说,其中2个注册ID已成功发送,一个没有,因为注册ID是错误的!

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

现在我的问题是:如何解析Google GCM响应,以获取尚未发送的注册ID?谷歌是否有一个应用程序接口或其他东西,以便我可以给它"multicat_id“,它会告诉我哪个注册ID有问题?

任何帮助都会非常感谢,我只是太困惑了:)

EN

回答 3

Stack Overflow用户

发布于 2013-06-04 23:22:10

它基于顺序:

假设您发送以下内容:

代码语言:javascript
运行
复制
{ "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"]
}

然后从Google那里得到这样的回复:

代码语言:javascript
运行
复制
{ "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) ID未注册。这两个都应该从数据库中删除。

票数 15
EN

Stack Overflow用户

发布于 2013-07-03 08:41:19

下面是我是如何做到的:

代码语言:javascript
运行
复制
$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.";

    }
}
票数 9
EN

Stack Overflow用户

发布于 2015-04-01 18:16:22

基于我在这里读到的:

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

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

代码语言:javascript
运行
复制
$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;

                    }
                }
            }
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16921189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档