在Android中从FCM获取自定义有效载荷

在Android中从FCM获取自定义有效载荷

本文介绍了在Android中从FCM获取自定义有效载荷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从FCM推送通知中阅读以下提到的键.

I have to read below mentioned keys from FCM Push notification.

    {
      "from_id": "",
      "data": {
        "sender_id": "15",
        "receiver_id": "42",
        "sender_name": "Addy",
        "alert": "Addy sent you a message.",
        "notification_type": "message",
        "message": "testing"
              }
     }

我正在使用

      public void onMessageReceived(RemoteMessage remoteMessage) {

      String title=remoteMessage.getData().get("sender_name");

      sendNotification(title, "");

      }

问题是 title = remoteMessage.getData.get("sender_name");返回null ,当我在浏览器中查看时,它显示数据存在于 sender_name 中.

Problem is title=remoteMessage.getData.get("sender_name"); returns null , and when I view in browser it shows me that data is present in sender_name.

推荐答案

public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.e(TAG, "From: " + remoteMessage.getFrom());

        if (remoteMessage == null)
            return;

        // Check if message contains a notification payload.
        if (remoteMessage.getNotification() != null) {
            Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
            handleNotification(remoteMessage.getNotification().getBody());
        }

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

            try {
                JSONObject json = new JSONObject(remoteMessage.getData().toString());
                handleDataMessage(json);
            } catch (Exception e) {
                Log.e(TAG, "Exception: " + e.getMessage());
            }
        }
    }

这篇关于在Android中从FCM获取自定义有效载荷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 11:52