Firebase onMessageReceived() 未从 http 请求中调用

Firebase onMessageReceived() not called from http request

发现从 Firebase 控制台发送通知后,如果应用程序在后台,则不会调用 onMessageReceived() 方法。

因此,我听从了人们的建议,使用了 HTTP POST 请求。 我一直在使用 postman 发出 post 请求

https://fcm.googleapis.com/fcm/send

与 2 headers:

Content-Type: application/json

权限:key=ALsa.......6ryLiI

和body

 {
 "to" : "fzgh-aHiOd8:APA91bFcKuwuVq_2gbLydHkAZf9XLUzm9vPalh7EUQT3Kyj8mcW9raAs9QvxbHaGJ4-U4RnUJwJ3UaEarbMXiQ8rHnsx9UuYBDrOI9tJYB78_Z3VVR1l7H_6PGpCkdgINx8a4vvMXD9q",
"notification" : {
    "body" : "Sales!",
    "title" : "ID Phone",
    "icon" : "myicon"
},
"data" : {
    "data" : "sales",

}

}

现在,通知确实进入了我的应用程序(这很好),但它仍然不是我想要的自定义通知。即 onMessageReceived() 仍然没有被调用。

有什么我遗漏的吗?

您应该定义数据负载以在 onmessagereceived() 中获取消息。通知标签仅用于通知。

从 GCM 查看这个 documentation:

Data messages

Set data with your custom key/value pairs to send a data payload to the client app. Data messages can have a maximum 4KB payload.

For example, here is a JSON-formatted message in the same IM application as above, where the information is encapsulated in data and the client app is expected to interpret the content:

 {
   "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
   "data" : {
     "Nick" : "Mario",
     "body" : "great match!",
     "Room" : "PortugalVSDenmark"
   },
 }

编辑

替换这些

Content-Type - application/json
Authorization - key=ALsa.......6ryLiI

Content-Type:application/json
Authorization:key=ALsa.......6ryLiI