Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FCM Data messages: need to send JSON array in place of String value

As per firebace doc, we can send FCM data messages in below format:

{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMEFQ3P1...",
"data":{
  "Nick" : "Mario",
  "body" : "great match!",
  "Room" : "PortugalVSDenmark"
}
}
}

But, I am required to send data as JSON Array like below:

{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoIZvvDMExUdFQ3P1...",
"data":{
       "geofence":[{
                     "Nick" : "Mario",
                     "body" : "great match!",
                    "Room" : "PortugalVSDenmark"
                    },
                    {
                    "Nick" : "Mario",
                    "body" : "great match!",
                    "Room" : "PortugalVSDenmark"
                    }]
}
}
}

I am getting below error if I try to send JSON Array in request body for sending messages:

Unable to send message to Firebase:

  {  "error": 
   {    "code": 400,    
"message": "Invalid value at 'message.data[0].value' (Map), Cannot have repeated items ('geofence') within a map.\nInvalid JSON payload received. Unknown name \"\" at 'message.data[0].value': Proto fields must have a name.",    
"status": "INVALID_ARGUMENT",    
"details": [      {        "@type": "type.googleapis.com/google.rpc.BadRequest",        "fieldViolations": [          {            "field": "message.data[0].value",            "description": "Invalid value at 'message.data[0].value' (Map), Cannot have repeated items ('geofence') within a map."          },          {            "field": "message.data[0].value",            "description": "Invalid JSON payload received. Unknown name \"\" at 'message.data[0].value': Proto fields must have a name."          }        ]      }    ]  
}} 

Android code is expecting data in JSON Array format not in String. I am stucked here badly.

Any help will be highly appreciated.

like image 275
S Singh Avatar asked Dec 19 '18 06:12

S Singh


1 Answers

The values in the data payload must be strings. You can't have arrays or objects. Your geofence is an array.

You can see from the Android API for RemoteMessage.getData() that the return type is Map<String, String>, which implies that you can only have string values. You can see also in the protocol docs that the data payload is string keys mapped to string values. So, you're going to have to figure out a way to flatten your data to strings.

like image 76
Doug Stevenson Avatar answered Nov 15 '22 05:11

Doug Stevenson