简体   繁体   中英

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())

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM