繁体   English   中英

当应用程序在Kotlin中处于前台时通知不会显示

[英]Notification not showing when app is in foreground in Kotlin

我在Kotlin中使用Firebase推送通知,以下是用于显示推送通知的代码段

 mNotifyManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        createChannel(mNotifyManager)
    val mBuilder = NotificationCompat.Builder(this, "bks-channel")
            .setLargeIcon(largeIcon)
            .setContentTitle("Bks")
            .setSmallIcon(R.drawable.app_icon)
            .setContentText(message)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
    mNotifyManager.notify(getRandomNumber(), mBuilder.build())

并且createChannel()函数是:

    @TargetApi(26)
private fun createChannel(notificationManager: NotificationManager)
{
    val name = "bks"
    val description = "bks"
    val importance = NotificationManager.IMPORTANCE_DEFAULT

    val mChannel = NotificationChannel(name, name, importance)
    mChannel.description = description
    mChannel.enableLights(true)
    mChannel.lightColor = Color.BLUE
    notificationManager.createNotificationChannel(mChannel)
}

以下是服务器日志:

 array(1) {
  [9]=>
  array(2) {
    ["name"]=>
    string(14) "ABL Staff USER"
    ["fcm_response"]=>
    array(2) {
      ["fields"]=>
      array(3) {
        ["data"]=>
        array(4) {
          ["click_action"]=>
          int(2)
          ["title"]=>
          string(19) "Attendance Reminder"
          ["body"]=>
          string(49) "Hi ABL Staff USER, Please mark your attendance ! "
          ["sound"]=>
          string(7) "default"
        }
        ["registration_ids"]=>
        array(11) {
          [0]=>
          string(152) "device_token_1"
        }
        ["notification"]=>
        array(4) {
          ["click_action"]=>
          int(2)
          ["title"]=>
          string(19) "Attendance Reminder"
          ["body"]=>
          string(49) "Hi ABL Staff USER, Please mark your attendance ! "
          ["sound"]=>
          string(7) "default"
        }
      }

同样适用于android o以下的前景和背景,但不适用于android o或以上的环境

FCM已发送两种通知

  1. 通知讯息
  2. 数据信息

发送数据消息以在后台运行应用程序时显示通知

有关更多详细信息,请检查以下链接

Firebase数据消息

您要通过名称“ bks-channel”创建频道吗? 看起来您不是在创建bks-channel,而是在通过名称bks创建频道。

更新您的创建频道方法。

 @TargetApi(26)
  private fun createChannel(notificationManager: NotificationManager)  {
    val name = "bks-channel"
    val description = "bks"
    val importance = NotificationManager.IMPORTANCE_DEFAULT
    val mChannel = NotificationChannel(name, name, importance)
    mChannel.description = description
    mChannel.enableLights(true)
    mChannel.lightColor = Color.BLUE
    notificationManager.createNotificationChannel(mChannel)
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM