NotificationChannel的setSound方法设置失效

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值