我正在创建 android 通知频道,我希望通知振动并播放声音。但由于某种原因,通知总是显示在 android 下拉菜单中的 Silent 组下。不播放声音或振动。这是我使用的代码,
val channelId = getString(R.string.default_notification_channel_id)
val channelName = getString(R.string.default_notification_channel_name)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val bigTextStyle = NotificationCompat.BigTextStyle()
.bigText(messageBody)
.setBigContentTitle(messageTitle)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_icon)
.setColor(getColor(R.color.deeper_blue))
.setStyle(bigTextStyle)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(pendingIntent)
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
channelId,
channelName,
NotificationManager.IMPORTANCE_HIGH
)
val att = AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_ALARM)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build()
channel.enableLights(true)
channel.lightColor = getColor(R.color.deeper_blue)
channel.enableVibration(true)
// Vibration Pattern if vibration is enabled
val VIBRATION_DURATION = 1000L
val WAITING_DURATION = 2000L
channel.vibrationPattern = longArrayOf(WAITING_DURATION, VIBRATION_DURATION, WAITING_DURATION, VIBRATION_DURATION)
channel.setSound(defaultSoundUri,att)
notificationManager.createNotificationChannel(channel)
}
val rnds = (0..1000).random()
notificationManager.notify((rnds * System.currentTimeMillis()).toInt() /* ID of notification */, notificationBuilder.build())
我正在运行 android 11 的 Google Pixel 4a 5G 上进行测试。下面是来自 android 设置的屏幕截图,这是默认创建通道的方式
如果我切换到默认值,它会开始提醒和振动,但是在安装应用程序时,它不会自动设置为默认值。