[英]FCM Push Notifications are only displayed when app is in background
I am using the following code to receive and display push notifications:我正在使用以下代码接收和显示推送通知:
override fun onMessageReceived(remoteMessage: RemoteMessage) {
if (remoteMessage.getNotification() != null) {
var title : String = remoteMessage.notification!!.title!!
var message : String = remoteMessage.notification!!.body!!
val intent = Intent(this, LoginCommonActivity::class.java)
intent.addflags(intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
var builder: NotificationCompat.Builder;
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
//val notificationManager = NotificationManagerCompat.from(applicationContext)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel =
NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannels(notificationChannel)
builder = NotificationCompat.Builder(applicationContext, notificationChannel.id)
} else {
builder = NotificationCompat.Builder(applicationContext)
}
builder = builder.setSmallIcon(R.drawable.ic_app_logo_black)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setDefaults(Notification.DEFAULT_ALL)
.setVibrate(longArrayOf(1000, 1000, 1000, 1000))
.setOnlyAlertOnce(true)
.setContentIntent(pendingIntent)
notificationManager.notify(System.currentTimeMillis().toInt(), builder.build())
}
}
But PNs' are only displayed when app is in background or closed.但 PN 仅在应用程序处于后台或关闭时显示。 Debugging the
FCMMessagingService
, I can see that PNs' are being sent by the server and received in onMessageReceive()
.调试
FCMMessagingService
,我可以看到 PN 正在由服务器发送并在onMessageReceive()
中接收。 It appears that the notify()
method is not working, or something else in the code is failing. notify()
方法似乎不起作用,或者代码中的其他内容失败了。
As per this article , FCM notifications are handled by the internal Firebase service when the app is in background, else they are received in onMessageReceived()
of FirebaseMessagingService
when the app is in the foreground, and we have to manually display them with notify()
.根据这篇文章,当应用程序在后台时,FCM 通知由内部 Firebase 服务处理,否则当应用程序在前台时,它们会在
FirebaseMessagingService
的onMessageReceived()
中接收,我们必须使用notify()
手动显示它们. But this isn't working.但这不起作用。
We've already seen this problem before here , here , here and here .我们已经在这里、 这里、 这里和这里看到过这个问题。 But that was maybe when the old GCM features were still present in FCM.
但那可能是旧的 GCM 功能仍然存在于 FCM 中的时候。 By this time FCM must have been completely overhauled.
到这个时候,FCM 一定已经彻底检修过了。
My questions are:我的问题是:
NotificationManager.notify()
function in the first place?NotificationManager.notify()
函数的原因是什么? If push notifications only appear when the app is in the background, and they are auto-handled by the Firebase service, then why is this method there at all? Can someone please point out where the problem is?有人可以指出问题出在哪里吗?
I will try to answer all your questions我会尽力回答你所有的问题
First notification are of two types 1): Notification Payload ( Your's case) 2): Data Payload第一个通知有两种类型 1):通知有效负载(您的情况) 2):数据有效负载
1): Notification Payload 1): 通知负载
In notification payload -If App in Background ( FCM will handle push notifications it self ) -If App in Foreground ( payload will receive in onMessageReceived we have to write our own logic to weather show notification or not )在通知有效负载中-如果应用程序在后台(FCM 将自行处理推送通知)-如果应用程序在前台(有效负载将在 onMessageReceived 中接收,我们必须编写自己的逻辑来天气显示通知)
2): Data Payload In data payload -If App in Background ( payload will receive in onMessageReceived we have to write our own logic to weather show notification or not ) -If App in Foreground ( payload will receive in onMessageReceived we have to write our own logic to weather show notification or not ) 2):数据负载在数据负载中 - 如果后台应用程序(负载将在 onMessageReceived 中接收,我们必须编写自己的逻辑来天气显示通知) - 如果应用程序在前台(负载将在 onMessageReceived 中接收,我们必须编写自己的天气显示通知的逻辑与否)
For Notification Please try using updated code from android guide using Compat library通知请尝试使用 android 指南中的更新代码,使用 Compat 库
https://developer.android.com/training/notify-user/build-notification#groovy https://developer.android.com/training/notify-user/build-notification#groovy
Just remove "notification" key from payload and provide only "data" key.只需从有效负载中删除“通知”键并仅提供“数据”键。
Application handles notification messages only if the app is in foreground but it handles data messages even if the application is in background or closed.应用程序仅在应用程序处于前台时才处理通知消息,但即使应用程序处于后台或已关闭,它也会处理数据消息。
more info: https://stackoverflow.com/a/38850030/19892188更多信息: https ://stackoverflow.com/a/38850030/19892188
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.