“registration_ids” field is not a JSON array (Fire

2019-04-12 07:27发布

I am facing problem with Firebase "registration_ids". When I send the request from Rest Client i get the successful response.

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

But when I call same php script from my Android application it gives me error in the response. (I got the response in Charles Proxy)

"registration_ids" field is not a JSON array

Here is my php script

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();
}

4条回答
对你真心纯属浪费
2楼-- · 2019-04-12 07:56

I had the similar scenario where I had to create a GCM group for user's registration tokens & that error occurred. I figured it out later that i was not following corrent JSON format mentioned on fire-base

Solution:

Create a model

public class tempo {
        public string operation {get;set; }

        public string notification_key_name { get; set; }

        public List<string> registration_ids { get; set; }
    }

then serialize it

  var hmm = Newtonsoft.Json.JsonConvert.SerializeObject(obj);

and make a post call to google.

查看更多
Animai°情兽
3楼-- · 2019-04-12 07:59

If you Used registration_ids you need to passed data in array as follow:

PHP:

$fields = array(
        'registration_ids' => array($registration_ids),
        'data' => $message,
    );
查看更多
爷的心禁止访问
4楼-- · 2019-04-12 08:07

If registration_ids is just one Token as stated at https://firebase.google.com/docs/cloud-messaging/http-server-ref you should use the field 'to'.

FCM requires registered_ids to be a JSON Array with indexes starting from 0. I have the same error because the indexes where not these. I solved it using the PHP function array_values.

function sendPushNotification($registration_ids, $message) {

   ignore_user_abort();
   ob_start();

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

   //FCM requires registration_ids array to have correct indexes, starting from 0
   $registration_ids = array_values($registration_ids);

   $numTokens = count($registration_ids);
   if($numTokens == 1){
      $fields = array(
        'to' => $registration_ids[0],
        'data' => $message,
      );
   }elseif($numTokens > 1){
      $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();
}
查看更多
三岁会撩人
5楼-- · 2019-04-12 08:15

I used to rather than sending array of Firebase tokens as registration_ids. Cause registration_ids having the limit of 1000 number of firebase tokens only 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();
}
查看更多
登录 后发表回答