GCM 注册无效错误


gcm invalid registration error

我向服务器传递一个昵称,然后通过昵称从我的数据库中获取注册ID。

GOOGLE_API_KEY是由Google GCM API生成的服务器密钥。

这是服务器的 PHP 代码:

<?php
require_once("db_config.php");

$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD) or      
die(mysqli_error());
$db = mysqli_select_db($con, DB_DATABASE) or die(mysqli_error()) or die(mysqli_error());

$nicknames = $_POST['nicknames'];
$message = $_POST['message'];


$registration_ids = mysqli_query($con, "SELECT regId FROM Users WHERE nickname = '$nicknames'");
$numOfRows = mysqli_num_rows($registration_ids);
$row=mysqli_fetch_assoc($registration_ids );
printf ("%s'n",$row["regId"]);

if($numOfRows > 0) {
$url = 'https://gcm-http.googleapis.com/gcm/send';
//$url = 'https://android.googleapis.com/gcm/send';
$fields = array('registration_ids' => array($registration_ids), 'data' => array("message" => $message),);
$headers = array('Authorization: key=' . GOOGLE_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);
    // Disabling SSL Certificate support temporarly
    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;
}
else {
    echo 'false';
}     
?>

发送消息后,我收到:

{"multicast_id":4772095405461687926,"成功":0,"失败":1,"canonical_ids":0,"结果":[{"错误":"无效注册"}]}]

我还打印了

我从数据库中获得的注册表ID,它与设备的注册ID匹配。

泰的帮助。

好的,

我解决了我的问题。问题是在这一行"array($registration_ids)"中,$registration_ids是一个结果对象,所以我把它改成了$row["regId"],然后我得到了注册ID。

现在它工作了。

您应该检查要传递给服务器的注册令牌。请参阅下游消息错误响应代码。

您还可以查看此文档,其中建议检查注册令牌的有效性。