如何解析GCM响应从php删除服务器上的无效注册ID

Pey*_*man 12 php android push-notification google-cloud-messaging

我对Google Cloud Messaging有疑问......

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

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

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

任何帮助都会非常感激,我只是很困惑:)

Era*_*ran 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"]
}
Run Code Online (Sandbox Code Playgroud)

并得到Google的回复:

{ "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"}
  ]
}
Run Code Online (Sandbox Code Playgroud)

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


小智 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.";

    }
}
Run Code Online (Sandbox Code Playgroud)