How can I make a notification that doesn\'t make a sound when I build it? I am building a notification, and my users don\'t like the fact that it makes a sound.
I might be late but still wants to add this . You can disable sound using .setSound(null)
on NotificationCompat.Builder builder
for all OS below O.
For O version n above add channel.setSound(null,null)
after creating NotificationChannel channel
All the solutions above mine is either outdated or covers some OS versions only
use that exact code:
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_DEFAULT);
NOTIFICATION_CHANNEL_ID --> random String.
channelName ==> random string
NotificationCompat.Builder.setSilent(true)
This works regardless of the Notification Channel setting. This allows you to have a channel that makes sound by default but allows you to post silent notifications if desired without making the entire channel silent.
Reference: https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#setSilent(boolean)
To disable the sound in OREO 8.1, change the priority of the notification as LOW and it will disable the sound of notification:
NotificationManager.IMPORTANCE_LOW
The code is like:
NotificationChannel chan1 = new NotificationChannel("default", "default", NotificationManager.IMPORTANCE_LOW);
Remove the line to builder.setDefaults(Notification.DEFAULT_ALL);
. It will not play the sound, but you may need to enable all other notification defaults if preferred