简体   繁体   English

应用程序在前台时收到的推送通知与应用程序在后台时收到的推送通知不同

[英]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. 例如,假设您想在收到用户通知时执行特定的任务,或者在后台执行某项操作而用户没有意识到或不想向用户显示通知对话框,则当应用程序处于有背景的。

Message Types 讯息类型

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的自定义键值对。

Solution: 解:

If you want to handle notification when the application is backgrounded you should send data message and use onMessageReceived method. 如果要在应用程序后台运行时处理通知,则应发送数据消息并使用onMessageReceived方法。

Here is the complete article. 这是完整的文章。

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.

相关问题 如果应用程序处于前台,则不会收到sendbird推送通知 - sendbird push notification not received if app is in foreground Firebase 在 android 中从后台删除应用程序时未收到推送通知 - Firebase push notification not received when app remove from background in android 应用程序处于前台时未收到推送通知 ios flutter - Push Notification not received when app is in foreground ios flutter 前台应用程序抖动时未收到 Firebase 推送通知? - Firebase Push Notification not received when flutter app in foreground? 如何使用IBM Mobilefirst接收到推送通知时知道应用程序是在后台还是在前台 - how to know if app is in background or foreground when push notification is received using IBM Mobilefirst 收到推送通知时打开Android应用 - Open Android App when Push Notification is received 应用程序被终止时,设备未收到推送通知 - push notification not received to devices when app is killed 当通过fireBase收到推送通知时,IONIC2在android后台应用程序时增加徽章 - IONIC2 Increment badges while app in background on android when push notification is received via fireBase 无论应用是后台还是前台,均未收到FCM通知 - FCM notification not received whether the app in background or foreground 当应用程序不在后台或前台时,无法将通知推送到通知管理器 - When the app is not in background or foreground, not able push Notification to Notification manager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM