首页 > 解决方案 > 当应用程序在 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或更高版本

标签: androidkotlin

解决方案


FCM 已发送两种类型的通知

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

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

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

Firebase 数据消息


推荐阅读