使用NotificationManagerCompat
和NotificationCompat
时,是否可以在Android Oreo上设置 channel ?
最佳答案
由于NotificationManagerCompat
只是使生活更轻松的包装器类,因此可以正常创建 channel :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = getString(R.string.channel_title)
val description = getString(R.string.channel_description)
val importance = NotificationManager.IMPORTANCE_HIGH
val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
mChannel.description = description
mChannel.enableLights(true)
mChannel.lightColor = Color.parseColor("#5B3C88")
mChannel.enableVibration(true)
mChannel.vibrationPattern = longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)
val manager = (context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager)
manager.createNotificationChannel(mChannel)
}
然后在发布通知时使用
NotificationManagerCompat
,但不要忘记使用新的构造函数构造通知:NotificationCompat.Builder(context, CHANNEL_ID)
关于android - Android Oreo上的NotificationManagerCompat,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45878921/