FCM onReceivedMessage() 获取 Bundle

Eds*_*eis 3 android firebase firebase-cloud-messaging

结构链接

我正在尝试从我的 ArrayMap 获取消息,但我无法访问 ReceiveMessage 包。我试图直接访问Map,但是很错误

我的代码

public class FbService extends FirebaseMessagingService {
    public FbService() {
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO(developer): Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.

        Map<String, String> params = remoteMessage.getData();
        String message =  params.get(3);

        Log.d("FbService", "Notification Message Body: " + message);



    }
}
Run Code Online (Sandbox Code Playgroud)

mah*_*her 5

感谢@Pritish Joshi

我找到了这个答案并帮了我很多,这是代码片段

您以 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());
          }
       }
Run Code Online (Sandbox Code Playgroud)

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

这是演示 Json 文件

{
  "to": "registration_ids",
  "data": {
    "key": "value",
    "key": "value",
    "key": "value",
    "key": "value"
  }
}
Run Code Online (Sandbox Code Playgroud)

参考 :