【发布时间】:2018-04-12 22:03:16
【问题描述】:
我正在使用 FCM。通知工作正常,onMessageReceived 在前台被调用,但是当我在应用程序处于后台/被杀时收到通知时,该方法永远不会被调用。
我正在使用 Firebase 文档建议的数据消息,但没有成功...
【问题讨论】:
标签: android firebase notifications firebase-cloud-messaging
我正在使用 FCM。通知工作正常,onMessageReceived 在前台被调用,但是当我在应用程序处于后台/被杀时收到通知时,该方法永远不会被调用。
我正在使用 Firebase 文档建议的数据消息,但没有成功...
【问题讨论】:
标签: android firebase notifications firebase-cloud-messaging
FCM 有 3 种通知
1.通知消息
Json 正文将只有 notification 标签
如果应用程序在后台,FCM 将自动在客户端应用程序中显示通知。 onMessageReceived() 将在前台调用。在后台,onMessageReceived() 不会被调用。
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
}
2。数据消息
Json body 将只有 data 标签
开发人员必须显示通知。 onMessageRecieved 将在前台和后台调用。 json 中应该只有数据标签
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
}
}
}
3.通知和数据消息
json 正文将同时具有 notification 和 data 标记。 onMessageReceived() 只有在应用程序处于前台时才会被调用。如果应用在后台,通知将自动显示,并且不会调用 onMessageReceived()。
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
},
"data" : {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
}
}
更多信息请阅读https://firebase.google.com/docs/cloud-messaging/concept-options
【讨论】:
这是我要发送的通知:
payload = new JSONObject();
notification = new JSONObject();
notification.put("title", "Avisos");
notification.put("text", "Nuevos avisos disponibles");
payload.put("data", notification);
payload.put("to", "/topics/avisos");
payload.put("priority",10);
【讨论】:
您确定要添加这样的服务吗? AndroidManifest.xml
<service android:name=".services.fcm.FirebaseService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
【讨论】: