Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Notification Channel always defaults to Silent

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.

like image 718
Easy Coder Avatar asked Oct 26 '25 14:10

Easy Coder


2 Answers

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

Add this to your Manifest.xml

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

like image 71
gcantoni Avatar answered Oct 29 '25 03:10

gcantoni


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

like image 32
Jetwiz Avatar answered Oct 29 '25 05:10

Jetwiz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!