52

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.

How can I change it to a silent one / no sound at all?

How I show notification:

android.support.v7.app.NotificationCompat.Builder builder = new android.support.v7.app.NotificationCompat.Builder(main);
builder.setStyle(new android.support.v7.app.NotificationCompat.BigTextStyle().bigText(text));
builder.setSmallIcon(R.drawable.app);
builder.setContentTitle("Rooster Maandag:");
builder.setOngoing(false);
builder.setAutoCancel(true);
builder.setSilent(true);
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
notificationManager = (NotificationManager) main.getSystemService(main.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());

I tried to search on google, but the only results I get is HOW to play a sound, not HOW to not play a sound...

Edit It possibly is a duplicate in some people's eyes, but in mine I could not find out an alternative for the there specified default, while this new method is called setDefaults

Amit Bodaliya
  • 128
  • 2
  • 4
Jason
  • 1,658
  • 3
  • 20
  • 51
  • Possible duplicate of [Android : Notification sound disable](http://stackoverflow.com/questions/7655164/android-notification-sound-disable) – Pavneet_Singh Oct 01 '16 at 17:55
  • @PavneetSingh Sorry, hadn't come up with the word "disable" – Jason Oct 01 '16 at 18:05
  • Kind of blows my mind I have to waste time researching how to get a silent notification. I tried this (on Xamarin) and no luck: notificationBuilder.SetPriority(NotificationCompat.PriorityMin); – Gerry Mar 18 '19 at 15:09
  • Seems you can't change a channel's priority, so I had to create two channels, one Low and the other Default. – Gerry Mar 18 '19 at 15:35
  • https://stackoverflow.com/questions/45919392/disable-sound-from-notificationchannel/45920861 – kotoMJ Mar 27 '19 at 20:09

11 Answers11

44

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);
Adinia
  • 3,722
  • 5
  • 40
  • 58
Kuldip Sharma
  • 782
  • 1
  • 6
  • 8
  • 10
    As mentioned in different thread of the same question: PRIORITY_LOW (No sound for Android 7.1 and lower) IMPORTANCE_LOW(No sound for Android 8.0 and higher) But the most important part is: once channel is created, it is not possible to change it’s priority, unless app goes uninstalled!!! – kotoMJ Mar 27 '19 at 20:07
  • @kotoMJ and also deleting the channel and recreating it does nothing either. – lasec0203 Jun 18 '20 at 02:07
  • @lasec0203 Deleting a channel and recreating it with a different ID, works. Recreating it with the same ID will restore the settings for the channel (un-deletes it). https://developer.android.com/reference/android/app/NotificationManager#deleteNotificationChannel(java.lang.String) – Melvin Dec 25 '22 at 08:59
  • Sidenote: Changing the ID to get around the loophole, circumvents the point of the notification channel system. If a user had set a channel to a specific importance, the developer should respect that user preference. – Marvin Dec 27 '22 at 05:24
34

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)

Marvin
  • 777
  • 8
  • 17
  • 2
    Thanks, I used notificationBuilder.setNotificationSilent() – Na Pro Jan 22 '21 at 02:55
  • No problem! FYI `notificationBuilder.setNotificationSilent()` is deprecated. – Marvin Apr 06 '21 at 20:47
  • 3
    Wow all these convoluted answers and you come out with this. Thank you! – user5646514 Jun 27 '21 at 18:21
  • 1
    this should be the top answer. – Imed Boumalek Aug 26 '22 at 17:20
  • Does this type of notification let the foreground service be actually foreground? – Mohsin Falak Nov 28 '22 at 10:18
  • For Oreo and Above: While this was a good and simple answer, it doesn't seem to work as expected for notifications that also have the method setFullScreenIntent() method (for high priority - CALL or ALARM type notifications). adding setSound(null, null) to the NotificationChannel works as intended (after deleting the existing notification channel and recreating it with a different ID) – Melvin Dec 25 '22 at 09:28
31

It works for me in Android Oreo.

You should just write your channel like this:

NotificationChannel notificationChannel = new NotificationChannel("Id" , "Name", NotificationManager.IMPORTANCE_DEFAULT);
            notificationChannel.setSound(null, null);
            notificationChannel.setShowBadge(false);
            notificationManager.createNotificationChannel(notificationChannel);
Михаил
  • 498
  • 4
  • 10
  • 1
    wow, I created a blank mp3 but looks like I could just use `.SetSound(null, null)` on the `NotificationChannel` ... thanks so much! +1 also works in `Xamarin.Android` – hexagod Nov 19 '19 at 01:28
  • 1
    This is perfectly working without affect the priority of the notification. – Priyankchoudhary Dec 28 '20 at 09:16
18

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

Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
  • 1
    It will then vibrate... I want it to have no feedback at all – Jason Oct 01 '16 at 18:03
  • When I remove the line it just displays the notification :) . Thank you so much – Jason Oct 01 '16 at 18:04
  • What if you want to retain the builder itself and just update the acoustic feedback back and forth? I tried several answers in this post but nothing helps: notificationCompatBuilderInstance!!.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY) .setGroupSummary(false) .setDefaults(NotificationCompat.DEFAULT_LIGHTS) – bdevay Dec 08 '20 at 23:19
  • There is a new API in AndroidX that specifically handles this scenario now: https://stackoverflow.com/a/64811427/875834 – Marvin Jan 23 '21 at 16:32
17

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

rana
  • 1,824
  • 3
  • 21
  • 36
  • 1
    For pre-O, you can also call .setVibrate(null) on the builder to keep it from vibrating. – Matt Oct 29 '19 at 18:29
10

In android O, for me it worked with this settings in the notification:

                .setGroupAlertBehavior(GROUP_ALERT_SUMMARY)
                .setGroup("My group")
                .setGroupSummary(false)
                .setDefaults(NotificationCompat.DEFAULT_ALL)
Ignacio Tomas Crespo
  • 3,401
  • 1
  • 20
  • 13
  • 2
    And for C#: Notification.Builder builder = new Notification.Builder(this) .SetSmallIcon(Resource.Drawable.btn_icon_header) .SetCustomContentView(contentView) .SetChannelId(URGENT_CHANNEL) .SetContentIntent(contentIntent) .SetGroupAlertBehavior(NotificationGroupAlertBehavior.Summary) .SetGroup("My group") .SetGroupSummary(false); – innomotion media Oct 29 '18 at 11:10
  • This alone made the notification display without sound? That doesn't seem right, as DEFAULT_ALL is supposed to do sound, lights, and vibration: https://developer.android.com/reference/android/app/Notification.html#DEFAULT_ALL – Mark Nov 08 '19 at 00:45
  • @Mark the official docs sometimes are not up to date, or not accurate for all the OS versions. I suggest you try the code yourself. – Ignacio Tomas Crespo Nov 20 '19 at 16:30
7

i had used this piece of code in NotificationCompat.Builder try this,

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setNotificationSilent();
6

Use this If you want all (sound, vibration and lights) in notifications.

builder.setDefaults(Notification.DEFAULT_ALL);

Or you can enable or disable items based on your requirements.

builder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);

comment this line if you want nothing.

albeee
  • 1,452
  • 1
  • 12
  • 20
6

Easy way to go to be able to show notification with any kind of priority is to set some sound that is silence actually. Just generate some silence.mp3 put it in "raw" folder and set notification sounds using Uri:

Uri.parse("android.resource://"+YOUR_APP_PACKAGE_NAME+"/"+R.raw.silence) 

You can generate this .mp3 with app like Audacity. It has option generate silence, just set how many seconds and you are good to go.

If you set defaults to 0 and set sound to null, notification will be shown without you hearing it but you wont be able to show notifications with some higher priority.

user1540907
  • 191
  • 2
  • 5
0

use that exact code:

NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_DEFAULT);

NOTIFICATION_CHANNEL_ID --> random String.
channelName ==> random string
0

You can set more than one channel. In my case, my aplication has a background service with two sounds notifications, and one notification without sound. I get it with this code:

//creating channels:

    NotificationChannel channel1 = new NotificationChannel(CHANNEL_ID_1, "CarNotification1", NotificationManager.IMPORTANCE_HIGH);
    NotificationChannel channel2 = new NotificationChannel(CHANNEL_ID_2, "CarNotification2", NotificationManager.IMPORTANCE_MIN);
    NotificationChannel channel3 = new NotificationChannel(CHANNEL_ID_3, "CarNotification3", NotificationManager.IMPORTANCE_HIGH);

    //mSound1 and mSound2 are Uri
    channel1.setSound(mSound1, null);
    channel3.setSound(mSound2, null);

and when I create the notification:

String channelId;
switch (situation){
    case situation2:
        channelId=CHANNEL_ID_2;
        break;
    case situation1:
        channelId=CHANNEL_ID_1;
        break;
    default:
        channelId=CHANNEL_ID_3;
}

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
//etcetera
Carlos Gómez
  • 357
  • 4
  • 9