应用程序在前台时收到的推送通知与应用程序在后台时收到的推送通知不同
Push notification received while the app is in the foreground is different from push notification received when the app is in background
我在 android 应用程序中实现了 FCM 推送通知。
当我登录到应用程序时。我收到的通知格式如下所示。
当应用程序处于后台时,我收到 json 响应,如下所示。
以下是我在onMessageRecieved()中添加的代码
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("App")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap))/*Notification with Image*/;
如何在两种情况下以相同的方式获得通知。
任何帮助将不胜感激谢谢
之所以如此,是因为推送通知在 前景 和 背景 中的处理方式不同。
Notification messages delivered when your app is in the background. In
this case, the notification is delivered to the device’s system tray.
A user tap on a notification opens the app launcher by default.
Messages with both notification and data payload, both background and
foreground. In this case, the notification is delivered to the
device’s system tray, and the data payload is delivered in the extras
of the intent of your launcher Activity.
另外你要记住there are 2 types of firebase pushes - 通知消息和数据消息。
看起来您正在使用 Notification
消息,因此它们按预期工作。
只需使用 data
类型的消息,因此您可以随时根据需要处理它们。
在Android 应用程序中设置FCM 后,您可以使用Firebase 控制台发送通知。当前台应用程序收到通知时,将调用 onMessageReceived 方法。您应该覆盖此方法来处理通知,但问题是当应用程序在后台接收通知时,通知会传送到设备的系统托盘,您无法使用 onMessageReceived 方法处理通知。当用户点击通知时,默认情况下会打开应用程序启动器。例如,考虑到你想在收到用户通知时执行特定任务,或者在用户没有意识到的情况下在后台执行某些操作,或者不想向用户显示通知对话框,你不能在应用程序运行时执行这些操作有背景。
消息类型
使用 FCM,您可以向客户端发送两种类型的消息:
1-通知消息,有时会想到“显示消息”
2-数据消息,由客户端应用程序处理
根据 Google 文档,通知消息有 2KB 的限制和预定义的用户可见键。数据消息允许开发人员发送最多 4KB 的自定义键值对。
解决方案:
如果您想在应用程序后台处理通知,您应该发送数据消息并使用 onMessageReceived 方法。
我在 android 应用程序中实现了 FCM 推送通知。
当我登录到应用程序时。我收到的通知格式如下所示。
当应用程序处于后台时,我收到 json 响应,如下所示。
以下是我在onMessageRecieved()中添加的代码
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("App")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap))/*Notification with Image*/;
如何在两种情况下以相同的方式获得通知。
任何帮助将不胜感激谢谢
之所以如此,是因为推送通知在 前景 和 背景 中的处理方式不同。
Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.
Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
另外你要记住there are 2 types of firebase pushes - 通知消息和数据消息。
看起来您正在使用 Notification
消息,因此它们按预期工作。
只需使用 data
类型的消息,因此您可以随时根据需要处理它们。
在Android 应用程序中设置FCM 后,您可以使用Firebase 控制台发送通知。当前台应用程序收到通知时,将调用 onMessageReceived 方法。您应该覆盖此方法来处理通知,但问题是当应用程序在后台接收通知时,通知会传送到设备的系统托盘,您无法使用 onMessageReceived 方法处理通知。当用户点击通知时,默认情况下会打开应用程序启动器。例如,考虑到你想在收到用户通知时执行特定任务,或者在用户没有意识到的情况下在后台执行某些操作,或者不想向用户显示通知对话框,你不能在应用程序运行时执行这些操作有背景。
消息类型
使用 FCM,您可以向客户端发送两种类型的消息:
1-通知消息,有时会想到“显示消息”
2-数据消息,由客户端应用程序处理
根据 Google 文档,通知消息有 2KB 的限制和预定义的用户可见键。数据消息允许开发人员发送最多 4KB 的自定义键值对。
解决方案:
如果您想在应用程序后台处理通知,您应该发送数据消息并使用 onMessageReceived 方法。