【问题标题】:Get value from RemoteMessage from FCM onMessageReceived method通过 FCM onMessageReceived 方法从 RemoteMessage 获取值
【发布时间】:2016-07-07 11:29:41
【问题描述】:

我已迁移 gcm to fcm 以获取推送通知消息。 但是我如何从接收到的远程消息中获取捆绑数据的 onMesssageReceived 方法。

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

所以请告诉我如何解析远程消息以获取通知的所有值。

我的工资单

{
"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"
]
}

【问题讨论】:

标签: java android google-cloud-messaging android-notifications firebase-cloud-messaging


【解决方案1】:

这里的代码 sn-p 几乎可以自我解释。

你以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());
          }
       }

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

这里是演示 Json 文件

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

【讨论】:

  • 感谢您的回答,但我在通知对象下的其他数据不在数据对象中,那么我该如何获取它。
  • remoteMessage.getData() 返回空白数组
【解决方案2】:

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

以下是我在应用程序中使用的方式,其中数据是我的 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();

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

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

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

【讨论】:

  • 感谢您的回答,但我在通知对象下的其他数据不在数据对象中,那么我该如何获取它
  • 正如您在评论中已经提到的,您将从 remoteMessage.getNotification() 获得结果。你能试试吗..
  • 没有message.get函数
  • get 方法属于地图。您需要先调用 RemoteMessage.getData()。
  • 别忘了在remoteMessage.getData.get()上调用toString()方法。
【解决方案3】:

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

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

你应该让他们通过

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

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

【讨论】:

    【解决方案4】:

    乐趣的类型getData()Map&lt;String, String&gt;。所以你可以像我们通常使用Map&lt;String, String&gt; 那样获取data 对象值。

    "data": {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3",
        "key4": "value4"
      }
    

    那么你必须在 kotlin 中这样获取

    val data = remoteMessage.data
    val key1 = data[key1]
    val key2  = data[key2]// you can fetch like this
    
    

    对于getNotification(),类型为Notification 对象

    val notification: Notification =  remoteMessage.notification
    val message = notification.body
    val title = notification.title // you can fetch by accessing Notification class member
    
    

    这里是Notification对象的详细信息

    【讨论】:

      【解决方案5】:

      RemoteMessage.Notification notification = remoteMessage.getNotification();
              Map<String, String> data = remoteMessage.getData();
              sendNotification(notification, data);
      String urlparam=data.get("url_param").toString();
      String urlparam=data.get("dp_name").toString();
      
      "data": {
          "notification_id": 11,
          "title_url": "service_id=1;service_category=Installation;",
          "destination": "Category_page"
        }
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-01
        • 1970-01-01
        • 2022-07-03
        相关资源
        最近更新 更多