2

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

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
4

1 回答 1

8

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

解决方法:清除应用数据或直接卸载应用,然后就可以正常使用了

于 2019-05-17T08:08:17.303 回答