GoogleCloudMessaging PHP脚本始终返回invalidregistration

时间:2013-07-31 11:33:21

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

我知道有很多帖子都有相同的问题,但在阅读完所有帖子之后我找不到问题的原因。

我写了一个GCM客户端来注册我的设备以接收来自我服务器的消息。它工作正常,我可以将注册ID存储在我的数据库中。

我的问题出在服务器端。我正在使用google搜索的脚本,但我总是收到错误结果:

{"multicast_id":7341760174206782539,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}

PHP代码是(我在教程中使用的是BROWSER API而不是SERVER API,但尝试两个键都会返回相同的消息):

<?php
// Replace with real BROWSER API key from Google APIs
$apiKey = "AIzaSyACln4edhSZW30XcmYpqoMz_gcRCC1iFjY";

// Replace with real client registration IDs 
$registrationIDs = array( "APA91bEdf1w4dQtsUqPT1jHhWEpvrpxzB1yrpL3RVtKrVxfzxfg2-Yl-pwHorsnmSnkqywQ8G90YcGEBoqCjgQU8CnjA0N7mOWF8bHMhHAs4ty46PPTX8yh6eSaSqvU3JTMmb-P0ma90EBG0rsQQbUh3aX895KxitI3LCiGOYqRfE5pZQ");

// Message to be sent
$message = "this is a test";

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

$fields = array(
                'registration_ids'  => $registrationIDs,
                'data'              => array( "message" => $message ),
                );

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

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
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_POSTFIELDS, json_encode( $fields ) );

// Execute post
$result = curl_exec($ch);

// Close connection
curl_close($ch);

echo $result;
?>

我把真实的ID和密钥放在一起,看看我做得对。有什么想法吗?

8 个答案:

答案 0 :(得分:8)

几分钟前我遇到了同样的问题。

更改此

$fields = array(
            'registration_ids'  => $registrationIDs,
            'data'              => array( "message" => $message ),
            );

到此:

$fields = array(
             'registration_ids'  => array($registrationIDs),
             'data'              => array( "message" => $message ),
             );

答案 1 :(得分:3)

我发现了我的问题。我的PHP库从post或get变量中删除了一些字符。其中一个特性是'_',因为它可以用作SQL注入。那是我的问题。固定!

答案 2 :(得分:2)

我遇到了同样的问题。我的问题是我的gcm_regid。 gcm_regid的长度为183个字节。但是,我的数据库使用150个字节来存储此值。当然存储的gcm_regid是不完整的。我通过增加密钥的存储大小来解决问题。然后问题就解决了。

答案 3 :(得分:1)

您可以使用此代码:

<?php

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


$registrationIds = array( $_POST['id'] );

// prep the bundle
$msg = array
(
    'message'   => 'here is a message. message',
    'title'     => 'This is a title. title',
    'subtitle'  => 'This is a subtitle. subtitle',
    'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
    'vibrate'   => 1,
    'sound'     => 1,
    'largeIcon' => 'large_icon',
    'smallIcon' => 'small_icon'
);

$fields = array
(
    'registration_ids'  => $registrationIds,
    'notification'          => $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.$registrationIds[0];

您可以根据需要将notification更改为data

答案 4 :(得分:0)

只需用此替换代码即可。 确保将YOUR_REGISTRATION_IDYOUR_MESSAGEYOUR_API_KEYREGISTRATION_IDS和其他评论替换为原始。

<?php
$regId=YOUR_REGISTRATION_ID;
$msg=YOU_MESSAGE;
$message = array("message" => $msg);
$regArray[]=REGISTRATION_IDS;
$url = 'https://android.googleapis.com/gcm/send';

$fields = array('registration_ids' => $regArray, 'data' => $message,);
$headers = array( 'Authorization: key=YOUR_API_KEY','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);
echo $result; 
if($result==FALSE)
{
    die('Curl Failed');
}
curl_close($ch);
}
?>

答案 5 :(得分:0)

您确定要发送给Google的注册ID值是否正确?

  

注册ID无效

     

检查传递给服务器的注册ID的格式。确保它与com.google.android.c2dm.intent.REGISTRATION意图中手机收到的注册ID相匹配,并且您不会截断它或添加其他字符。       错误代码为InvalidRegistration时发生。

虽然我不知道有效注册ID的格式是什么,但我从未在注册ID中看到两个破折号之间带有2个字符的注册ID:...2-Yl-p...

答案 6 :(得分:0)

将数组数据发布到它..

前:

$registatoin_ids = array($regId);
$message = array(
            "msg_category"      => "cat001",
            "msg_identifier"    => "msg01",
            "user_id"           => "1234",
            "title"             => $title,
            );

然后调用函数..

$result = $gcm->send_notification($registatoin_ids, $message);
public function send_notification($registatoin_ids, $message) {
    // include config
    include_once './config.php';

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

    $fields = array(
        'registration_ids' => $registatoin_ids,
        'data' => $message,
    );

    $headers = array(
        'Authorization: key=' . GOOGLE_API_KEY,
        'Content-Type: application/json'
    );
    // Open connection
    $ch = curl_init();

    // Set the url, number of POST vars, POST data
    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_VERIFYHOST, 0);   

    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

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

    // Close connection
    curl_close($ch);
    echo $result;
}

答案 7 :(得分:0)

类似的问题,最后收到了错误的注册ID(令牌)。

我添加了以下行

echo "id=[$id]";

到PHP代码,验证ID是否与logcat中打印的令牌完全一致,并使用上面的回显线找到了ID的开头和结尾添加了空格。删除空格后,邮件发送时没有错误。