Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FCM Field "data" must be a JSON array

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?

like image 260
Daniel ORTIZ Avatar asked Sep 14 '25 07:09

Daniel ORTIZ


2 Answers

From the Firebase Cloud Messaging documentation, it seems like data should be a JSON object:

{
   "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
   "data" : {
     "Nick" : "Mario",
     "body" : "great match!",
     "Room" : "PortugalVSDenmark"
   },
 }

I'm not sure why the error message talks says it needs to be an array. It's like meant as an "associative array" which is really just another term for a JSON object.

like image 130
Frank van Puffelen Avatar answered Sep 15 '25 20:09

Frank van Puffelen


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.

like image 26
Siamak Avatar answered Sep 15 '25 20:09

Siamak