php - "registration_ids"字段不是 JSON 数组 (Firebase)

标签 php android firebase firebase-cloud-messaging

我正面临 Firebase“registration_ids”的问题。当我从 Rest Client 发送请求时,我得到了成功的响应。

{"multicast_id":4650719213012935695,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1468837777484579%214918aff9fd7ecd"},{"message_id":"0:1468837777484484%214918aff9fd7ecd"}]}

但是当我从我的 Android 应用程序调用相同的 php 脚本时,它会在响应中给出错误。 (我在 Charles Proxy 中得到了响应)

"registration_ids" field is not a JSON array

这是我的 php 脚本

function fetchFirebaseTokenUsers($message) {       
    $query = "SELECT token FROM firebase_tokens";
    $fcmRegIds = array();
    if($query_run = mysqli_query($this->con, $query)) {         
        while($query_row = mysqli_fetch_assoc($query_run)) {
            array_push($fcmRegIds, $query_row['token']);
        }
    }

    if(isset($fcmRegIds)) {
        $pushStatus = $this->sendPushNotification($fcmRegIds, $message);
    }
}

function sendPushNotification($registration_ids, $message) {

    ignore_user_abort();
    ob_start();

    $url = 'https://fcm.googleapis.com/fcm/send';

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

    define('GOOGLE_API_KEY', 'AIzaSyC.......VdYCoD8A');

    $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_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    $result = curl_exec($ch);
    if($result === false)
        die('Curl failed ' . curl_error());

    curl_close($ch);
    return $result;
    ob_flush();
}

最佳答案

我使用 to 而不是将 Firebase token 数组作为 registration_ids 发送。导致 registration_ids 仅具有 1000 个 firebase token 的限制 https://firebase.google.com/docs/cloud-messaging/http-server-ref .

function fetchFirebaseTokenUsers($message) {       
   $query = "SELECT token FROM firebase_tokens";
   $fcmRegIds = array();
   if($query_run = mysqli_query($this->con, $query)) {         
      while($query_row = mysqli_fetch_assoc($query_run)) {
        array_push($fcmRegIds, $query_row['token']);
      }
   }

   if(isset($fcmRegIds)) {
      foreach ($fcmRegIds as $key => $token) {
         $pushStatus = $this->sendPushNotification($token, $message);
      }
   }
}

function sendPushNotification($registration_ids, $message) {

   ignore_user_abort();
   ob_start();

   $url = 'https://fcm.googleapis.com/fcm/send';

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

   define('GOOGLE_API_KEY', 'AIzaSyC.......VdYCoD8A');

   $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_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

   $result = curl_exec($ch);
   if($result === false)
      die('Curl failed ' . curl_error());

   curl_close($ch);
   return $result;
   ob_flush();
}

关于php - "registration_ids"字段不是 JSON 数组 (Firebase),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38435097/

相关文章:

java - 自定义监听器

Firebase auth CDN 不导出模块

php - UTF-8贯穿始终

Android操作栏溢出菜单去除阴影

php - 这个 PHP 有什么问题?什么都没有出现

android - 在我的手机上更改 Android API 级别

Android - Firebase 邀请返回错误 - 结果代码 0 - 已发送电子邮件未发送短信

java - 如何在原始堆栈跟踪位置集中记录异常?

php - 如何从Chrome浏览器找到空白屏幕的PHP错误?

php - 插入两行时 stmt->execute 不起作用