gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:09:18 26 4
gpt4 key购买 nike

我在播放声音时遇到问题,当通知到来时,通知正在显示,但没有声音或振动,我看到很多关于如何将声音和振动设置为通知 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com