android 8.0 (Build.VERSION.SDK_INT >= 26)NotificationChannel设置setSound不生效的解决办法
使用NotificationChannel设置通知类别
总所周知,Android8.0及以上创建通知栏需要讲通知区分开来,每一类通知设置一种通知类别。因此NotificationChannel类就应运而生了。
NotificationChannel 的使用
private fun createNotificationChannel() :String{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val NOTIFY_CHANNEL = "MY_CHANNEL"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(NOTIFY_CHANNEL, "Notifications", importance)
channel.description = "This is a notification channel"
channel.setSound(
Uri.parse("android.resource://" + this.packageName + "/" + R.raw.alarm_note_voice),
Notification.AUDIO_ATTRIBUTES_DEFAULT
)
val notificationManager = getSystemService(NotificationManager::class.java)
notificationManager?.createNotificationChannel(channel)
return NOTIFY_CHANNEL
}
return ""
}
以上是创建NotificationChannel并返回channelId,NotificationChannel的第一个参数是channelId类别ID,第二个是channel渠道的名称,这个会显示在系统的通知管理里面,第三个参数就是通知的一个级别了。通过 notificationManager?.createNotificationChannel(channel)这个方法就设置了一个渠道,多个渠道就多次设置。
创建一个普通的通知
private fun createNotification() {
val builder = NotificationCompat.Builder(this,createNotificationChannel())
builder.setSmallIcon(R.drawable.ic_notifications_accent)
builder.setContentTitle("Sample Notification")
builder.setContentText("This is a sample Expanded Notification")
builder.setAutoCancel(true)
builder.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.user_woman_icon))
builder.setSound( Uri.parse("android.resource://" + this.packageName + "/" + R.raw.alarm_note_voice))
val notification = builder.build()
val mgr = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mgr.notify(NOTIFY_ID, notification)
}
创建一个文本内容多的通知
private fun createLargeNotification() {
val builder = NotificationCompat.Builder(this, createNotificationChannel())
builder.setSmallIcon(R.drawable.ic_notifications_accent)
builder.setContentTitle("Sample Large Notification")
builder.setContentText("More text here. Expand!")
builder.setAutoCancel(true)
builder.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.user_woman_icon))
val bigTextStyle = NotificationCompat.BigTextStyle()
bigTextStyle.setBigContentTitle("This is a Expand Notification")
bigTextStyle.bigText(resources.getString(R.string.long_msg))
builder.setStyle(bigTextStyle)
val notification = builder.build()
val mgr = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mgr.notify(NOTIFY_ID, notification)
}
创建可以扩展的通知,点击可以跳转的通知
private fun createActionNotification() {
val builder = NotificationCompat.Builder(this, createNotificationChannel())
val intent = Intent(this, NotificationResultActivity::class.java)
intent.putExtra(“notifyID”, NOTIFY_ID)
val pendingIntent = PendingIntent.getActivity(
this,
NOTIFY_ID,
intent,
PendingIntent.FLAG_CANCEL_CURRENT
)
builder.setSmallIcon(R.drawable.ic_notifications_accent)
builder.setContentTitle("Sample Large Notification")
builder.setContentText("More text here. Expand!")
builder.setAutoCancel(true)
builder.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.user_woman_icon))
builder.setSubText("Tap to view")
builder.setContentIntent(pendingIntent)
val bigTextStyle = NotificationCompat.BigTextStyle()
bigTextStyle.setBigContentTitle("This is a Expand Notification")
bigTextStyle.bigText(resources.getString(R.string.long_msg))
builder.setStyle(bigTextStyle)
builder.addAction(R.mipmap.ic_launcher, "Action 1", pendingIntent)
builder.addAction(R.mipmap.ic_launcher, "Action 2", pendingIntent)
builder.setVisibility(NotificationCompat.VISIBILITY_SECRET)
val notification = builder.build()
val mgr = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
mgr.notify(NOTIFY_ID, notification)
}
源码参考:https://github.com/jp5201314/NotificationChannel.git
总结
如果在调试过程中对channel 调用了setSound 还是不生效,因为channel setSound 默认只在第一次安装时生效,只需要删除APP重新安装即可。
[1]: https://stackoverflow.com/questions/48986856/android-notification-setsound-is-not-working
[2]:https://img-blog.csdnimg.cn/20201127113803270.png
[3]: https://stackoverflow.com/questions/46234254/android-oreo-notification-keep-making-sound-even-if-i-do-not-set-sound-on-older