How To Parse Json Data In Android For Firebase Cloud Messaging (fcm)
Solution 1:
try this code:
publicvoidonMessageReceived(RemoteMessage remoteMessage)
{
Log.e("DATA",remoteMessage.getData().toString());
try
{
Map<String, String> params = remoteMessage.getData();
JSONObjectobject = newJSONObject(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
Solution 2:
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 = newJsonObject(); // com.google.gson.JsonObjectJsonParser jsonParser = newJsonParser(); // com.google.gson.JsonParserMap<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)
Solution 3:
I have changed to
JSONObject json = newJSONObject(remoteMessage.getData());
from
JSONObject json = newJSONObject(remoteMessage.getData().toString());
and work fine.
Solution 4:
Since dealerInfo
is parsed as string and not an object, create a new JSONObject with the string
JSONObject dealerInfo = newJSONObject(object.getString("dealerInfo"));
String dealerId = dealerInfo.getString("dealerId");
String operationCode = dealerInfo.getString("operationCode");
Solution 5:
I didn't want to add GSON (as I use moshi) to make it working, so I made Kotlin method to form json string from remoteMessage's map, tested this on one example, so don't forget to test this implementation before using:
overridefunonMessageReceived(remoteMessage: RemoteMessage?) {
super.onMessageReceived(remoteMessage)
var jsonString = "{"
remoteMessage?.data?.let {
val iterator = it.iterator()
while (iterator.hasNext()) {
val mapEntry = iterator.next()
jsonString += "\"${mapEntry.key}\": "val value = mapEntry.value.replace("\\", "")
if (isValueWithoutQuotes(value)) {
jsonString += value
} else {
jsonString += "\"$value\""
}
if (iterator.hasNext()) {
jsonString += ", "
}
}
}
jsonString += "}"
println(jsonString)
}
privatefunisValueWithoutQuotes(value: String):Boolean{
return (value == "true" || value == "false" || value.startsWith("[") || value.startsWith("{") || value == "null" || value.toIntOrNull() != null )
}
Edit:
Even better approach is to form FCM data like:
notificationType: "here is ur notification type"
notificationData: {
//here goes ur data
}
That way we can retreive both values from map.
remoteMessage?.data?.let {
it["notificationData"]?.let {
jsonString = it.replace("\\", "")
}
}
We got clear json without "playing" around.
And we can then use notificationType
to convert json to the object that we need (as several notification data types can be passed sometimes)
Post a Comment for "How To Parse Json Data In Android For Firebase Cloud Messaging (fcm)"