在Android中收到的FCM数据有效载荷不是json格式 [英] FCM Data payload received in android not in json format

查看:63
本文介绍了在Android中收到的FCM数据有效载荷不是json格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Firebase获取的数据有效载荷不是json格式,而是从以下格式获取自定义键值对:

I am getting the data payload from the firebase not in json format, instead I am getting custom key-value pairs as following format:

Data Payload:{image=https://www.xxxx.xxx/get-profile-picture, message=This is a test message., senderName=Mathew John}

我必须使用Json解析来解析数据以进行进一步处理. 这是我的代码:

I have to parse the data using Json parsing for further processing. Here is my code:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
        try {
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            String title = remoteMessage.getData().get("senderName");
            System.out.println("raja" + title);
            String msg = remoteMessage.getData().get("message");
            System.out.println("raja" + msg);
            sendMessage(msg,title);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

推荐答案

我从Firebase获取的数据有效载荷不是json格式

I am getting the data payload from the firebase not in json format

Yes ,其行为符合预期.

Yes, Its behaving as intended.

因为,数据有效载荷包含custom key-value pairs而不是JSON格式

Because Data payload contains custom key-value pairs not a JSON format

我必须使用Json解析来解析数据以进行进一步处理.

I have to parse the data using Json parsing for further processing.

您需要使用Map<String, String>将数据有效载荷转换为JSONObject

You need to use Map<String, String> to convert data payload in to a JSONObject

检查以下示例

示例代码

Map<String, String> params = remoteMessage.getData();
JSONObject object = new JSONObject(params);
Log.e("JSON_OBJECT", object.toString());

这篇关于在Android中收到的FCM数据有效载荷不是json格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆