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

查看:19
本文介绍了在 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, Its behaving as intended.

因为数据负载包含自定义键值对而不是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天全站免登陆