【问题标题】:MissingRegistraton error when trying to send data to GCM尝试向 GCM 发送数据时出现 MissingRegistraton 错误
【发布时间】:2013-05-14 13:08:38
【问题描述】:

我搜索了很多,但找不到任何解决此问题的方法。

我正在使用 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 API 控制台报告系统,请求甚至不会一直到达服务器。

有人知道哪里出了问题吗?

【问题讨论】:

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


    【解决方案1】:

    您的请求应如下所示:

    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 API 控制台报告系统中。

    【讨论】:

    • UTF8 需要什么不同的密钥?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 2014-03-22
    • 1970-01-01
    • 2017-02-18
    • 2016-03-18
    • 2012-08-23
    相关资源
    最近更新 更多