我正在创建Android通知渠道,希望通知能够震动并播放声音。但出于某种原因,通知始终显示在安卓下拉菜单的“静音”组中。没有播放任何声音或震动。这是我使用的代码:
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设置的屏幕截图,这是默认创建的通道。 如果我切换到默认,它将开始发出警报和振动,但在安装应用程序时,它不会自动设置为默认。