【问题标题】:NotificationChannel setSound and vibration not workingNotificationChannel 设置声音和振动不起作用
【发布时间】:2019-05-16 16:10:42
【问题描述】:

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

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

【问题讨论】:

    标签: android kotlin notification-channel


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢。我花了大约半小时在谷歌上找到这个答案?
    猜你喜欢
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多