[英]Push notification received while the app is in the foreground is different from push notification received when the app is in background
I have implemented FCM push notifications in an android app. 我已经在Android应用中实现了FCM推送通知。
While I am logged in to the app. 当我登录到应用程序时。 I get the notification in the form that I am expecting as below.
我收到的通知格式如下。
When the app is in background I receive the json response instead as below. 当应用程序在后台运行时,我将收到json响应,如下所示。
Following is the code I have added in onMessageRecieved() 以下是我在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*/;
How can I get the notification in the same manner in both cases. 在两种情况下,我如何以相同的方式获取通知。
Any help will be appreciated thank you 任何帮助将不胜感激谢谢
After setting up FCM in Android application, you can use Firebase console to send notifications. 在Android应用程序中设置FCM之后,您可以使用Firebase控制台发送通知。 When foregrounded application receives a notification, onMessageReceived method is invoked.
当前台应用程序收到通知时,将调用onMessageReceived方法。 You should override this method to handle notification, but the problem is when the application is in the background and receives a notification, notification delivers to the device's system tray and you can not handle notification with onMessageReceived method.
您应该重写此方法以处理通知,但是问题是当应用程序在后台接收通知时,通知传递到设备的系统托盘,并且您无法使用onMessageReceived方法处理通知。 When the user taps on a notification, it opens the app launcher by default.
当用户点击通知时,默认情况下会打开应用启动器。 For example consider you want to do a specific task when a notification is received to the user or do something in background without the user realizing or don't want to show notification dialog to the user, you can't do these when the application is backgrounded.
例如,假设您想在收到用户通知时执行特定的任务,或者在后台执行某项操作而用户没有意识到或不想向用户显示通知对话框,则当应用程序处于有背景的。
With FCM you can send two types of messages to clients : 使用FCM,您可以向客户发送两种类型的消息:
1- Notification message, sometimes thought of a “display message” 1-通知消息,有时被认为是“显示消息”
2- Data message, which are handled by the client application 2-数据消息,由客户端应用程序处理
According to Google documents a notification message has a 2KB limit and a predefined user-visible keys. 根据Google文档,通知消息的限制为2KB,并且预定义了用户可见的密钥。 Data messages let developers send up to 4KB custom key-value pairs.
数据消息使开发人员最多可以发送4KB的自定义键值对。
If you want to handle notification when the application is backgrounded you should send data message and use onMessageReceived method. 如果要在应用程序后台运行时处理通知,则应发送数据消息并使用onMessageReceived方法。
That is so because push notifications handles in foreground and background in different ways. 之所以这样,是因为推送通知以不同的方式处理前台和后台 。
From the documentation 从文档中
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.
在这种情况下,通知将传递到设备的系统托盘,而数据有效载荷将在启动器活动的意图之外传递。
Also you have to remember that there are 2 types of firebase pushes - Notification message and Data message . 您还必须记住, firebase推送有两种类型 - 通知消息和数据消息 。
Looks like you are using Notification
messages, so they are working as expected. 看起来您正在使用
Notification
消息,因此它们按预期方式工作。
Just use data
type of messages and so you can always handle them as you need. 仅使用消息的
data
类型,因此您可以随时根据需要进行处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.