45

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

Jason
  • 1,519
  • 3
  • 17
  • 47
  • 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

42

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,660
  • 5
  • 41
  • 57
Kuldip Sharma
  • 732
  • 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
30

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);
Михаил
  • 470
  • 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
  • This is perfectly working without affect the priority of the notification. – Priyankchoudhary Dec 28 '20 at 09:16
20

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 Bernal
  • 544
  • 4
  • 17
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
  • 11,939
  • 6
  • 33
  • 61
  • 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 Bernal Jan 23 '21 at 16:32
16

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,664
  • 3
  • 19
  • 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,283
  • 1
  • 17
  • 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
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,412
  • 1
  • 12
  • 18
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
6

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

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setNotificationSilent();
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
  • 347
  • 4
  • 9