Android Notification Channel always defaults to Silent

3k views Asked by At

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 enter image description here

If I switch to default, then it will start to alert and vibrate, but when installing the app, its not setting to Default automatically.

3

There are 3 answers

1
gcantoni On

Regarding the vibration, have you added the permission for it?

Add this to your Manifest.xml

<uses-permission android:name="android.permission.VIBRATE" />

0
Pavel Shirokov On

I had the same problem with android 11. I tried changing the notification. As a result, changing from NotificationManager.IMPORTANCE_MAX to NotificationManager.IMPORTANCE_HIGH in the channel fixed the error. I don't know why this happened, but before, everything worked fine with NotificationManager.IMPORTANCE_MAX. I assume this is an android bug.

0
Jetwiz On

first, you have to change to NotificationManager.IMPORTANCE_HIGH then

when editing notification settings, if there is a channel notification ID, you must RENAME the CHANNEL ID because the channel can only be created but cannot be modified!

for example, we want to turn on the notification sound on the channel id named 1 in the version 1 application. then we want to turn off the notification sound in the version 2 application, we have to rename the channel id so that the notification will sound on.

see : Disable sound from NotificationChannel

see: https://stackoverflow.com/a/47144981/7531626