且构网

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

应用程序在后台时的 Android 通知

更新时间:2022-12-27 12:28:49

正如你在评论中所说:

当应用处于后台时,应用不采用 setNumber、setAutoCancel、setSmallIcon、setLargeIcon 选项

这是因为您使用通知负载发送仅在前台触发的通知.

It is because you are using notification payload to send the notification which only triggers on the foreground.

所以当你的应用在后台时它不会进入这个方法.

So when your app is in the background it does not enter this method.

要解决这个问题,您可以单独使用 data 负载:

to solve this you can use data payload alone:

"data": {
"titles": "New Title",
"bodys": "body here"
}

因为数据负载将进入 onMessageReceived()代码> 当你的应用在前台/后台时.

since the data payload will enter the onMessageReceived() when your app is in foreground/background.

然后在 fcm 中你可以这样做:

then in fcm you can do this:

  if (remoteMessage.getData().size() > 0) {

        title = remoteMessage.getData().get("titles");
        body = remoteMessage.getData().get("bodys");
    }