Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse Json data in android for Firebase Cloud Messaging (FCM)

i am using FCM for push messages and handling all incoming push notification in onMessageReceived. Now the issue is with parsing nested json that comes inside this function remoteMessage.getData()

I have following block coming as a push notification in device. content of data payload could be varied here it is dealer later on it can be productInfo

{
  "to": "/topics/DATA",
  "priority": "high",
  "data": {
    "type": 6,
    "dealerInfo": {
      "dealerId": "358",
      "operationCode": 2
    }
  }
}

this how i am parsing it

 if(remoteMessage.getData()!=null){

        JSONObject object = null;
        try {
            object = new JSONObject(remoteMessage.getData());       

        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

now i am getting data with blackslashes as remoteMessage.getData() returns Map<String,String> so probably my nested block is being converted in string not sure though.

{
  "wasTapped": false,
  "dealerInfo": "{\"dealerId\":\"358\",\"operationCode\":2}",
  "type": "6"
}

and if i write object = new JSONObject(remoteMessage.getData().toString()); then it got failed with following notification

{
  "to": "regid",
  "priority": "high",
  "notification" : {
      "body": "Message Body",
      "title" : "Call Status",
      "click_action":"FCM_PLUGIN_ACTIVITY"
   },
  "data": {
    "type": 1,
     "callNumber":"ICI17012702",
     "callTempId":"0",
      "body": "Message Body",
      "title" : "Call Status"
  }
}

error i get is

> org.json.JSONException: Unterminated object at character 15 of
> {body=Message Body, type=1, title=Call Status, callNumber=ICI17012702,
> callTempId=0}
like image 306
Hunt Avatar asked Feb 16 '17 12:02

Hunt


People also ask

How do I send a FCM data message?

For sending FCM notification payload you can use Firebase Cloud Messaging Tool in firebase console. And click on Send your first message. Then enter the Title and body field. If you wish to send it to a particular device then click on Send test message and enter the FCM registration token.

What is difference between APNs and FCM?

APNs needs to be used for push messaging when an application is in the background. Hence, FCM uses APNs to deliver messages when the message contains user visible payload, e.g., body, sound etc. FCM also delivers via APNs when content available is set.


2 Answers

try this code:

public void onMessageReceived(RemoteMessage remoteMessage)
    {
        Log.e("DATA",remoteMessage.getData().toString());
        try
        {
            Map<String, String> params = remoteMessage.getData();
            JSONObject object = new JSONObject(params);
            Log.e("JSON OBJECT", object.toString());
            String callNumber = object.getString("callNumber");
            //rest of the code
      }
   }

Also make sure your JSON is valid use This

like image 61
rafsanahmad007 Avatar answered Sep 19 '22 16:09

rafsanahmad007


Faced this issue when migrating from GCM to FCM.

The following is working for my use case (and OP payload), so perhaps it will work for others.

JsonObject jsonObject = new JsonObject(); // com.google.gson.JsonObject
JsonParser jsonParser = new JsonParser(); // com.google.gson.JsonParser
Map<String, String> map = remoteMessage.getData();
String val;

for (String key : map.keySet()) {
    val = map.get(key);
    try {
        jsonObject.add(key, jsonParser.parse(val));
    } catch (Exception e) {
        jsonObject.addProperty(key, val);
    }
}

// Now you can traverse jsonObject, or use to populate a custom object:
// MyObj o = new Gson().fromJson(jsonObject, MyObj.class)
like image 20
gonga Avatar answered Sep 20 '22 16:09

gonga