简体   繁体   English

FCM 推送通知仅在应用程序处于后台时显示

[英]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 服务处理,否则当应用程序在前台时,它们会在FirebaseMessagingServiceonMessageReceived()中接收,我们必须使用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:我的问题是:

  • What is the default protocol for handling push notifications if they arrive when the app is in the foreground?如果推送通知在应用程序处于前台时到达,处理推送通知的默认协议是什么?
  • Looking at other popular apps - WhatsApp, Google Pay, Zomato - I do not ever recall seeing a push notification when the app is in the foreground.查看其他流行的应用程序 - WhatsApp、Google Pay、Zomato - 我不记得当应用程序在前台时看到推送通知。 Does this mean that push notifications never appear in the tray when the app is in the foreground?这是否意味着当应用程序在前台时,推送通知永远不会出现在托盘中?
  • If so, then what is the reason for having the 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?如果推送通知仅在应用程序处于后台时出现,并且由 Firebase 服务自动处理,那么为什么要使用此方法呢? Is this just a relic of the old GCM library?这只是旧 GCM 库的遗物吗?

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.

相关问题 当应用程序处于前台或后台时如何使用 FCM 处理通知 - How to handle notifications with FCM when app is in either foreground or background FCM 通知仅在应用程序未打开且处于后台时在设备上发送 - FCM Notifications only sends on device when the application is not open and on background FCM能否在Electron App中推送通知 - Can FCM push notifications in Electron App Android 应用在后台无法处理 Firebase 推送通知 - Android app cannot handle Firebase push notifications when in background Firebase 推送通知在后台应用程序时显示错误图标 - Firebase push notifications show wrong icon when app in background 应用程序在后台时推送通知不起作用 - Push notifications not working while app is in background Android 12 firebase 当应用程序在后台时,推送通知不起作用 - Android 12 firebase push notifications don't work when app is in the background 没有 FCM 后端的 Android 推送通知 - Android push notifications without FCM Backend React Native 推送通知,FCM 表示令牌仅在 iOS 上无效(Android 有效) - React Native Push Notifications, FCM says token is invalid on iOS only (Android works) DRF,Firebase FCM。 发送推送通知 - DRF, Firebase FCM. Sending push notifications
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM