没有从推送到服务器端获得推送通知

Not getting push notification from push to server-side

我通过 php 代码从服务器端推送消息,我总共有 6 个 android 设备,当我通过我的 php 脚本推送消息时,我得到以下响应,我在 4 台设备中收到通知,但在 2 台设备中没有收到任何通知,我看到 "success":6,还有那两个 android 设备 ID 在结果 JSON 响应中打印,并且 "canonical_ids":2 PHP 脚本:

<?php 
$registatoin_ids = array("XXXXXXX","XXXXXXX","XXXXXXX","XXXXXXX","XXXXXXX","XXXXXXX","XXXXXXX")
define( 'API_ACCESS_KEY', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' );
                    $message    = array("notification_status"=>1,"message"=>"Hello Android");   // Response for android

                    $fields = array
                    (
                        'registration_ids'  =>  $registatoin_ids,
                        'data'              =>  array( "price" => $message )
                    );
                    $headers = array
                    (
                        'Authorization: key=' . API_ACCESS_KEY,
                        'Content-Type: application/json'
                    );
                    $ch = curl_init();
                    curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
                    curl_setopt( $ch,CURLOPT_POST, true );
                    curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
                    curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
                    curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
                    curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
                    $result = curl_exec($ch );
                    curl_close( $ch );
                    echo $result;
?>

回复:

{
"multicast_id": 5091283388403222000,
"success": 6,
"failure": 0,
"canonical_ids": 2,
"results": [
    {
        "registration_id": "XXXXXXXXXXXXXXXXXXXXXXXXXXX",
        "message_id": "0:1439532747953065%12153c37f9fd7ecd"
    },
    {
        "registration_id": "XXXXXXXXXXXXXXXXXXXXXXXXXXX",
        "message_id": "0:1439532747954730%12153c37f9fd7ecd"
    },
    {
        "message_id": "0:1439532747954278%12153c37f9fd7ecd"
    },
    {
        "message_id": "0:1439532747963956%12153c37f9fd7ecd"
    },
    {
        "message_id": "0:1439532747953665%12153c37f9fd7ecd"
    },
    {
        "message_id": "0:1439532747954836%12153c37f9fd7ecd"
    }
]
}

具有 Canonical ID 的两台设备已用新的注册 ID 替换了旧的注册 ID。 Canonical Id 是这个新的,您应该在以后的请求中使用它来发送通知。

来自官方文档:

[...] A canonical registration ID is the registration token of the last registration requested by the client app . This is the ID that the server should use when sending messages to the device.

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. (This is why is shows "success" for all your devices) Make sure to replace the registration token stored in your server with this canonical ID, as eventually the old registration token will stop working.

所以通常情况下,如果您尝试使用这两个新 ID 再次发送通知,设备应该会照常收到通知。