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