Java 从 FCM onMessageReceived 方法的 RemoteMessage 获取值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38244332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:08:12  来源:igfitidea点击:

Get value from RemoteMessage from FCM onMessageReceived method

javaandroidgoogle-cloud-messagingandroid-notificationsfirebase-cloud-messaging

提问by Jatin Patel

I have migrate gcm to fcmfor push notification message. but how I Get bundle data from RemoteMessage received onMesssageReceived method.

我已迁移gcm to fcm推送通知消息。但是我如何从 RemoteMessage 接收到 onMesssageReceived 方法中的包数据。

Old GCM give bundle data onMessageReceiced method but in FCM there is RemoteMessage data.

So please tell me how I parse remotemessage for get all value of notification.

所以请告诉我如何解析 remotemessage 以获取通知的所有值。

MY PAYROL

我的工资单

{
"collapse_key":"score_update",
"priority":"high",
"content_available":true,
"time_to_live":108,
"delay_while_idle":true,
"data": 
{ 
    "message": "Message for new task",
    "time": "6/27/2016 5:24:28 PM"
},
"notification": {
    "sound": "simpleSound.wav",
    "badge": "6",
    "title": "Test app",
    "icon": "myicon",
    "body": "hello 6 app",
    "notification_id" : "1140",
    "notification_type" : 1,
    "notification_message" : "TEST MESSAGE",
    "notification_title" : "APP"
  },
"registration_ids": ["cRz9SJ-gGuo:APA91bFJPX7_d07AR7zY6m9khQro81GmSX-7iXPUaHqqcOT0xNTVsOZ4M1aPtoVloLNq71-aWrMCpIDmX4NhMeDIc08txi6Vc1mht56MItuVDdA4VWrnN2iDwCE8k69-V8eUVeK5ISer"
]
}

采纳答案by Drup Desai

In FCM you received RemoteMessage instead of Bundle.

在 FCM 中,您收到的是 RemoteMessage 而不是 Bundle。

Below is the way I used in my application where data is my RemoteMessage

下面是我在我的应用程序中使用的方式,其中数据是我的 RemoteMessage

Map<String, String> data = remoteMessage.getData()
int questionId = Integer.parseInt(data.get("questionId").toString());
String questionTitle = data.get("questionTitle").toString();
String userDisplayName = data.get("userDisplayName").toString();
String commentText = data.get("latestComment").toString();

Below is my notification data which I am sending it from server

以下是我从服务器发送的通知数据

{
  "registration_ids": "",
  "data": {
    "questionId": 1,
    "userDisplayName": "Test",
    "questionTitle": "Test",
    "latestComment": "Test"
  }
}

So you have to parse each and every field as per your response. As I have debugged the code you will receive map in your RemoteMessage and cast those fields in appropriate data types as all those data comes as string.

因此,您必须根据您的回复解析每个字段。当我调试代码时,您将在 RemoteMessage 中接收映射并将这些字段转换为适当的数据类型,因为所有这些数据都以字符串形式出现。

回答by Pritish Joshi

Here is the code snippet which is pretty much self Explanatory.

这是代码片段,它几乎是自我解释的。

You get the data in the form of the Map

您以 Map 的形式获取数据

public void onMessageReceived(RemoteMessage remoteMessage)
        {
            Log.e("dataChat",remoteMessage.getData().toString());
            try
            {
                Map<String, String> params = remoteMessage.getData();
                JSONObject object = new JSONObject(params);
                Log.e("JSON_OBJECT", object.toString());
          }
       }

Make Sure from server you are sending data in correct format i.e. in the "data" key

确保从服务器您以正确的格式发送数据,即在“数据”键中

here is the demo Json file

这是演示 Json 文件

{
  "to": "registration_ids",
  "data": {
    "key": "value",
    "key": "value",
    "key": "value",
    "key": "value"
  }
}

回答by Al-Punk

For your data that looks like:

对于您的数据,如下所示:

"data":{ 
    "message": "Message for new task",
    "time": "6/27/2016 5:24:28 PM"
}

you should get them through

你应该让他们通过

Log.d(TAG, "Key Data : " +  remoteMessage.getData().get("message").toString());
Log.d(TAG, "Key Data : " +  remoteMessage.getData().get("time").toString());

Wrap them in try catch to be sure

将它们包裹在 try catch 中以确保