I am creating android notification channel, i want the notification to vibrate and play sound. But for some reason the notification is always displayed under Silent group in android pull down menu. No sound or vibration is played. Here is the code I use,
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())
I am testing on Google Pixel 4a 5G, running android 11. Below is teh screen shot from android settings, this is how the channel created be default
If I switch to default, then it will start to alert and vibrate, but when installing the app, its not setting to Default automatically.