android - NotificationChannel 设置声音和振动不起作用

标签 android kotlin notification-channel

我在播放声音时遇到问题,当通知到来时,通知正在显示,但没有声音或振动,我看到很多关于如何将声音和振动设置为通知 channel 的问答,但没有行不通,这就是我正在尝试的方式

val pattern = longArrayOf(0, 200, 60, 200)
val chatSound = Uri.parse("${ContentResolver.SCHEME_ANDROID_RESOURCE}://" + context.packageName + "/" + R.raw.chat_alert)
val mBuilder = NotificationCompat.Builder(context, CHANNEL_ID)
  .setSmallIcon(R.mipmap.ic_launcher)
  .setContentTitle(title)
  .setContentText(body)
val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

val notification = mBuilder.build()

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
  val mChanel = NotificationChannel(CHANNEL_ID, "test", NotificationManager.IMPORTANCE_HIGH)
  val audioAttributes = AudioAttributes.Builder()
    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
    .build()
  mChanel.setSound(chatSound, audioAttributes)
  mChanel.enableVibration(true)
  mChanel.enableLights(true)
  mChanel.vibrationPattern = pattern
  mNotificationManager.createNotificationChannel(mChanel)
}
mNotificationManager.notify(i, notification)

我正在为 Android Pie 测试它

targetSdkVersion 28

最佳答案

似乎第一次创建通知 channel 时,如果发生变化,它不会自动更新,就像我的情况一样,起初,我在没有声音和振动的情况下运行通知 channel 。

解决方法:清除应用数据或直接卸载应用,之后即可正常使用

关于android - NotificationChannel 设置声音和振动不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56172663/

相关文章:

java - 单击按钮后如何使弹出菜单后面的背景变暗?

android - 向 PreferenceActivity 添加 header

java - Android Studio - JAVA_HOME 未指向有效的 JVM 安装

kotlin - 如何使用随机 bool 值生成器生成均匀的随机整数生成器?

java - 在 Kotlin 中映射的可变参数

java - 应用程序空指针异常

java - 如何解决默认 Activity 未找到问题?

android - NotificationCompat.Builder() 不接受 Channel Id 作为参数

android - Android 8.0 如何检测通知 channel 被用户屏蔽