lubrum
lubrum

Reputation: 444

Disable sound in Android Notifications (without changing visual behavior)

I was able to remove sound with IMPORTANCE_LOW creating the channel, but notification was not showing up on the screen. I want notification to show itself like in IMPORTANCE_HIGH but with no sound. One probably possible hacky way to do that is creating a silent sound mp3 in raw folder and use it in notification.

Is there a proper way to only disable sound in Android Notifications without changing the visual behavior of notification itself? I am looking for a solution for Android versions between 4.4 and 11

Relevant piece of code of what I've tried so far:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(getString(R.string.channel_id), getString(R.string.channel_name), NotificationManager.IMPORTANCE_HIGH)
            channel.enableVibration(true)
            channel.setSound(null, null)
            mNotificationManager.createNotificationChannel(channel)
}

var builder = NotificationCompat.Builder(this, chId)
            .setSmallIcon(R.drawable.notification_alarm)
            .setContentTitle(getString(R.string.tx_title))
            .setContentText(message)
            .setAutoCancel(true)
            .setTicker(message)

builder.setDefaults(0)
//builder.setDefaults(Notification.DEFAULT_LIGHTS or Notification.DEFAULT_VIBRATE)
startForeground(ENotifications.id, builder.build())

Upvotes: 0

Views: 140

Answers (1)

SamanSepahvand
SamanSepahvand

Reputation: 86

put this code to your project's

 NotificationChannel notificationChannel = new NotificationChannel("IdNotif","NameNotif", 
 NotificationManager.IMPORTANCE_DEFAULT);
        notificationChannel.setSound(null, null);
        notificationChannel.setShowBadge(false);
        notificationManager.createNotificationChannel(notificationChannel);

i use notificationChannel.setSound(null, null); to disable sound and no use blank sound.

Upvotes: 0

Related Questions