Android NotificationManager.IMPORTANCE_HIGH不等于重要性级别紧急

时间:2019-02-28 10:30:57

标签: android notifications

here中明确指出,NotificationManager.IMPORTANCE_HIGH应该以“紧急”的形式显示给用户,发出声音并作为提示通知出现。

但是,情况并非如此,因为它向用户显示为“高”,并且不会发出声音或显示为提示通知(当应用程序在后台时)

enter image description here

当我现在将重要性级别手动设置为“紧急”时,它会按应有的方式工作(显示为提醒通知)

这是Android中的某种错误还是仅仅是网站中的错误,因为保护用户免受未经用户同意而发送紧急通知的应用程序的保护是有意义的?

我的通知代码如下(我在Orio上进行了测试):

private fun sendNotification(messageBody: String) {
    val intent = Intent(this, MainActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT)

    val channelId = "channel_id"
    val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
    val notificationBuilder = NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.notification)
            .setContentTitle("Notification")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
    val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel(channelId,
                "Channel human readable title",
                NotificationManager.IMPORTANCE_HIGH)
        notificationManager.createNotificationChannel(channel)
    }

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
}

0 个答案:

没有答案