尝试向GCM发送数据时出现MissingRegistration错误。

3

我搜索了很多,但都没有找到这个问题的解决方案。

我正在使用PHP服务器,并试图发送PushNotifications到我的Android应用程序。 但是当我在浏览器中尝试我的代码时,我会收到此错误:“Error = MissingRegistration”。

这是我运行的代码:

registration_ids = array($regId);
        $message = array(
            'hangMessage' => $message,
            'userId' => $user_id
            );

        $result = $gcm->send_notification($registration_ids, $message);

这是我调用的代码:

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

    $fields = array(
        'registration_ids' => $regisration_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_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));

    //Execute psot
    $result = curl_exec($ch);
    if($result == false){
        die('Curl failed: ' . Curl_error($ch));
    }

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

据Google APIs控制台报告系统显示,该请求甚至没有完全到达服务器。

有人知道可能出了什么问题吗?

1个回答

3

你的请求应该像这样:

Content-Type:application/json
Authorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA

{
  "registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
  "data" : {
    ...
  },
}

因此,我认为错误可能在这一行中:
'Content-Type= application/json'

尝试将=更改为:
由于内容类型头部无效,GCM服务器可能会假定内容类型的默认值(可能是application/x-www-form-urlencoded;charset=UTF-8),在这种情况下,注册ID需要一个不同的密钥,这就解释了MissingRegistration错误。顺便说一句,收到MissingRegistraton错误意味着请求已经到达GCM服务器。 GCM请求未在Google APIs控制台报告系统中记录。

UTF8所需的不同密钥是什么? - mcmillab

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接