FCM Field "data" must be a JSON array

Viewed 6402

Hi im working with postman to make my json object FCM message: But when i try to send:

    {  
   "to":"fzvihT7dFUI:APA91bFVhnWAxXVjlWiiHIs9ZUyL1DE2hZO6GpItJtReh3hcKF1kD6mLuQq9fNP9xvS5bOFWUOG-OW-uyOedc1C43m8jfvD4OOfsBYuMbM7t1-dZEy2kQcuv3gJw6dhneVus2AR_hQHQ",
   "data":[  
      {  
         "time":1501385514224,
         "CC":"1030626890"
      }
   ],
   "notification":{  
      "body":"SPO2:95 \nPulso:75",
      "title":"El paciente Daniel Ortiz nesecita asistencia"
   }
}

The response its:

Field "data" must be a JSON array: [{"CC":"1030626890","time":1501385514224}]

But i know the [{"CC":"1030626890","time":1501385514224}] its a array, i dont understand the problem. What i made wrong?

3 Answers

I might be too late but for those who still face this problem , following change saved me.

By adding JSON_FORCE_OBJECT to json_encode() it will add missing braces so it should be something like this:

json_encode($fields ,JSON_FORCE_OBJECT));

That's it.

In case you serialize sql/api results data that each item of the array has a numeric key using php, for example an array like:

$results = [
   ['name'=>'john', 'durname'=>'doe'],
   ['chuck'=>'john', 'durname'=>'Norris'],
]

Before turning into FCM data try:

$results = [
   ["time":1501385514224, "CC":"1030626890"]
   ["time":1501385514334, "CC":"1030126890"]
]

$fcmData = [  
   "to"=>"fzvihT7dFUI:APA91bFVhnWAxXVjlWiiHIs9ZUyL1DE2hZO6GpItJtReh3hcKF1kD6mLuQq9fNP9xvS5bOFWUOG-OW-uyOedc1C43m8jfvD4OOfsBYuMbM7t1-dZEy2kQcuv3gJw6dhneVus2AR_hQHQ",
   "data":[  
     "data" => $results
   ],
   "notification" => [  
      "body"=>"SPO2:95 \nPulso:75",
      "title"=>"El paciente Daniel Ortiz nesecita asistencia"
   ]
]

$fcmData = json_encode($fcmData);

// Send push notification here

In case you are using laravel with brozot/laravel-fcm, a good approach is this one.

Thje bottom line is that fcm/push notification data MUST be an associative array.

Related