且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当应用程序在后台运行或未运行时,推送通知将无法正常工作

更新时间:2022-12-27 11:27:08

使用FCM,您可以指定POST负载以发送到https://fcm.googleapis.com/fcm/send.在该有效负载中,您可以指定datanotification键,或同时指定两者.

With FCM you specify a POST payload to send to https://fcm.googleapis.com/fcm/send. In that payload you can specify a data or a notification key, or both.

如果有效负载仅包含data键,则您的应用程序将自行处理所有推送消息.例如.它们都交付给您的onMessageReceived处理程序.

If your payload contains only a data key, your app will handle all push messages itself. E.g. they are all delivered to your onMessageReceived handler.

如果您的有效载荷包含notification键,则当您的应用程序处于活动状态/处于前台时,您的应用程序将仅处理推送消息.如果不是(因此它在后台或完全关闭),则FCM会使用您在notification密钥有效负载中输入的值来为您显示通知.

If your payload contains a notification key, your app will handle push messages itself only if your app is active/in the foreground. If it is not (so it's in the background, or closed entirely), FCM handles showing the notification for you by using the values you put into the notification key payload.

请注意,从控制台(如Firebase控制台)发送的通知始终包含notification键.

Note that notifications sent from a console (like Firebase console), they always include a notification key.

看起来您想自己处理FCM消息,以便可以进一步自定义通知等,因此***不要在POST负载中包含notification键,这样所有推送消息都将传递到您的onMessageReceived.

Looks like you want to be handling the FCM messages yourself so you can customize the notification a bit more etc, so it would be better to not include the notification key in the POST payload, so all push messages are delivered to your onMessageReceived.

您可以在此处了解更多信息:
高级消息传递选项
下游消息语法

You can read more about it here:
Advanced messaging options
Downstream message syntax