如何在 firebase 云消息 JSON 中获取 'to' 字段?

How to get 'to' field in firebase could messaging JSON?

我可以成功地将 Firebase 云消息发送到我的 Android 示例项目。作为 ,我应用了第 2 步并且到目前为止它有效。

我的JSONbody:

{
    "to": "/topics/testTopic",
    "data": {
        "key1" : "val1",
        "key2" : true
     }
}

有一个数据字段,我可以在我的 onMessageReceived() 方法中正确访问它:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    String toVal = remoteMessage.getTo();
    Map<String, String> data = remoteMessage.getData();
}

但是 getTo() 方法为空。 如何访问原始 JSON 中的 'to' 字段?

我期望在 im 变量 toVal 中有 "/topics/testTopic"

我想你在找 RemoteMessage.getFrom():

Get the sender of this message.

This will be the sender ID or the topic for topic messages.