在php代码中推送通知错误InvalidRegistration?

时间:2015-12-25 17:30:41

标签: php push-notification google-cloud-messaging

我有使用php代码推送通知的问题我已经尝试了很多东西,但我找不到代码中的错误。它告诉我InvalidRegistration的错误。我在我的旧项目中尝试了很多设备ID。

这是推送通知的PHP代码。

$devicesId = 'eHRFURsiMfg:APA91bEISYn7wFGczYLfVtq4j3fso2vcjAIbX32ACEjNDOrj_3Ra-BQg49jfpuNGsFpS_ucp_tEujse9yow_yTREKHCRD_y7a1kZZmq38L-YsHVTCnf0kdDR8_yN2IxcRPJNmRMHmr37';

    $url = 'https://android.googleapis.com/gcm/send';

    $fields = array(
        'registration_ids' => array($devicesId),
        'data' => array("message"=>'Breaking news.'),
    );

    $headers = array(
        'Authorization: key='."AIzaSyBL20ZXbSqlHGt8bU4Isv7ziHvylHkW4oU",
        'Content-Type: application/json'
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    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);

    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }

    curl_close($ch);
    echo $result; 

1 个答案:

答案 0 :(得分:0)

请重新查看您的device_id和授权密钥。以下代码工作正常。试试这个。

// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'AIzaSyAJQWiK9w2UVsVCY8P8pnOr356bHero6eo');

// prep the bundle
$msg = array('message'  => 'You liked this message!',
             'title' => 'title');

$fields = array(
'registration_ids'  => array('APA91bGJ0n6nhpp4F6kfTuJd4vZlS53mR4KYyylUHIbglSfGTjp3WhOtM37zDfYSN5BQGSTifE_27cRKDiNZD6ioXVUbY_BEXRi6dOQRRNWCApGvGy2F4lonAOXXCzMc7OXU8ipzgMqv'),
'data'   => $msg);

$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;