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

apeeds0o  于 2022-12-23  发布在  Kotlin
关注(0)|答案(2)|浏览(126)

我正在使用以下代码接收和显示推送通知:

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())
    }
}

但是PN '只在应用程序处于后台或关闭时显示。调试FCMMessagingService时,我可以看到PN'正在由服务器发送并在onMessageReceive()中接收。看起来notify()方法不起作用,或者代码中的其他东西失败了。
根据this article,当应用在后台时,FCM通知由内部Firebase服务处理,否则当应用在前台时,它们在onMessageReceived()FirebaseMessagingService中接收,我们必须使用notify()手动显示它们。
我们在herehereherehere之前已经看到过这个问题,但那可能是旧的GCM特性仍然存在于FCM中的时候,到那时FCM一定已经彻底检修了。
我的问题是:

  • 如果推送通知在应用处于前台时到达,则处理推送通知的默认协议是什么?
  • 看看其他流行的应用程序--WhatsApp、Google Pay、Zomato--我不记得在应用程序处于前台时看到过推送通知。这是否意味着当应用程序处于前台时,推送通知永远不会出现在托盘中?
  • 如果是这样的话,那么首先是什么原因让NotificationManager.notify()函数出现呢?如果推送通知只在应用程序处于后台时出现,并且它们是由Firebase服务自动处理的,那么为什么会有这个方法呢?这只是旧GCM库的遗物吗?

有人能指出问题出在哪里吗?

v09wglhw

v09wglhw1#

我会尽力回答你所有的问题
首次通知分为两种类型1):通知有效负载(您的案例)2):数据有效载荷

1):通知有效负载

在通知有效负载中-如果应用程序在后台(FCM将自行处理推送通知)-如果应用程序在前台(有效负载将在onMessageReceived中接收,我们必须编写自己的逻辑来显示天气预报通知与否)

2):数据有效负载在数据有效负载中-如果应用程序在后台(有效负载将在onMessageReceived中接收,我们必须将自己的逻辑写入天气显示通知或不)-如果应用程序在前台(有效负载将在onMessageReceived中接收,我们必须将自己的逻辑写入天气显示通知或不)

对于通知请尝试使用更新的代码从Android指南使用Compat库
https://developer.android.com/training/notify-user/build-notification#groovy

qoefvg9y

qoefvg9y2#

只需从有效负载中删除“通知”键,并仅提供“数据”键。
应用程序仅在应用程序处于前台时处理通知消息,但即使应用程序处于后台或关闭,它也会处理数据消息。
更多信息:https://stackoverflow.com/a/38850030/19892188

相关问题