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

时间:2018-11-20 06:26:33

标签: android 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或以上的环境中则不能使用

2 个答案:

答案 0 :(得分:0)

FCM发送了两种通知

  1. 通知消息
  2. 数据消息

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

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

Firebase data message

答案 1 :(得分:0)

您是否通过名称“ 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)
  }