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

    {
      "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"); returns null,当我在浏览器中查看时,它显示出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());
            }
        }
    }

关于java - 从Android中的FCM获取自定义有效载荷,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40583248/

10-12 19:30