php - 尝试将数据发送到 GCM 时出现 MissingRegistraton 错误

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

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

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

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

最佳答案

您的请求应如下所示:

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

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

因此我认为错误可能出在这一行:

'Content-Type= application/json'

尝试将 = 更改为 :

由于内容类型 header 无效,GCM 服务器可能会假定内容类型的默认值(可能是 application/x-www-form-urlencoded;charset=UTF-8,在这种情况下,注册 ID 需要一个不同的 key ,这将解释 MissingRegistration 错误。

顺便说一句,您收到 MissingRegistraton 错误意味着请求确实到达了 GCM 服务器。 GCM 请求未记录在 Google API 控制台报告系统中。

关于php - 尝试将数据发送到 GCM 时出现 MissingRegistraton 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16544075/

相关文章:

php - 解析 mySQL 查询结果并为 if/else 形成数组

c# - .NET 实例化错误

php - 在没有插件 reCAPTCHA 的情况下验证用户的答案

php - 如何用 if 条件解决 JavaScript 中的 setTimeout 问题?

android - 获取空值作为响应

iphone - 苹果推送通知在生产中不起作用

java - Maven 中的 FirebaseApp

java - 防止目录混淆

android - 折叠工具栏。如何调整自定义布局而不是默认的 ImageView

android - 应用程序关闭后,WorkManager 停止调度周期性 Worker