Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable sound from NotificationChannel

Today I started targeting API 26 which forced me to use Notification Channels.

My problem is that now on each new notification (including updates to it) an annoying sound is played.

How can I disable this sound?

I tried replacing this sound with a custom mp3 sound in order to then pass it a mp3 with silence in it, but this is ignored.

I'm just adding a notification which is of very low priority, basically giving the user the option to perform some actions after he has interacted with the app. There's no reason to be loud, the user will know that he can refer to the notification because he has done a certain thing with the app which he knows that will cause a notification to appear.

The user will really start getting annoyed by that sound.

like image 379
Daniel F Avatar asked Aug 28 '17 12:08

Daniel F


People also ask

How to disable Notification sound in Android studio?

Step 1: Tap to open the Settings app. Step 2: Tap Sound & Notifications. Step 3: Tap App Notifications. Step 4: Tap to open an app and then tap the toggle next to Block to disable or enable its notifications.

What is System silent channel?

Silent: Your phone won't make a sound or vibrate. But the notification will show up when you swipe down from the top of your screen.


3 Answers

If you want to keep the importance of your channel and just remove the sound notificationChannel.setSound(null, null); seems to do the job.

EDIT: Make sure to change the channel ID (and delete the old one) to have it applied to existing users. (Channels can be created, but never modified by the app, only the user can.)

like image 70
mVck Avatar answered Oct 19 '22 13:10

mVck


(Update 2019-05: It gets worse with Android Q, there I'm even getting a sound when using NotificationManager.IMPORTANCE_LOW, at least in the Emulator...)


The solution is to use NotificationManager.IMPORTANCE_LOW and create a new channel for it. Once a channel is created, you can't change the importance (well, you can, but the new importance is ignored). The channel information appears to get stored permanently by the system and any channel created is only deleted when you uninstall the app. [Update: According to Ferran Negre's comment, you can delete the channel via nm.deleteNotificationChannel(nChannel.getId()); and recreate it with nm.createNotificationChannel(nChannel); but apparently there's a limitation that you can't create a channel with the same id of a deleted channel and expect to be able to apply different settings to the undeleted channel, see acoder's answer]

While previous Android versions played no sound back by default, this changed with Android O, but only when you target the API 26, that is, use Notification Channels. This is an inconsistency, well, actually, it's a bug:

The reason for this is that when you create a channel with NotificationManager.IMPORTANCE_DEFAULT (not soundworthy by default) Android will actually "somewhat" register it as NotificationManager.IMPORTANCE_HIGH (plays sound by default).

You can check this by going into the options of the notifications (long press on the notification entry), where you will get to read that it is of type NotificationManager.IMPORTANCE_HIGH and then disable the notification and then re-enable it. In this process it gets "downgraded" from the NotificationManager.IMPORTANCE_HIGH to the non-sounding, actually registered NotificationManager.IMPORTANCE_DEFAULT.

The bug has been submitted to the Android issue tracker, so you may want to star it (flagged by Google as "Won't Fix (Infeasible)", because... spoiled).


BTW, the new docs at https://developer.android.com/training/notify-user/channels claim that the default behavior used to be that way, that default played a sound prior to Android 8.0, which is definitely not true. This is their list

User-visible importance level           Importance               Priority   
                                        (Android 8.0 and higher) (Android 7.1 and lower)
Urgent  Makes a sound and appears as    IMPORTANCE_HIGH          PRIORITY_HIGH
        a heads-up notification                                  or PRIORITY_MAX
High    Makes a sound                   IMPORTANCE_DEFAULT       PRIORITY_DEFAULT
Medium  No sound                        IMPORTANCE_LOW           PRIORITY_LOW
Low     No sound and does not appear    IMPORTANCE_MIN           PRIORITY_MIN
        in the status bar

You can even see the mismatch between visibility importance high and notification importance high... I don't know why they are doing this. They definitely have a bug in their code.


Everything below the next line is obsolete, yet that bug mentioned there is still valid. My error there was to think that NotificationManager.IMPORTANCE_MIN is the next lower one from NotificationManager.IMPORTANCE_DEFAULT, but NotificationManager.IMPORTANCE_LOW is.


When you then go into the notification settings of the app via long-press-notification and all-channels button and toggle the switch for that channel off and on again, then it actually sets itself to NotificationManager.IMPORTANCE_DEFAULT and no sound will get played. I also noticed that after a crash it did get reset to NotificationManager.IMPORTANCE_HIGH

So basically the workaround is to use NotificationManager.IMPORTANCE_MIN. But you have to create a new channel so that this NotificationManager.IMPORTANCE_MIN is in effect, because it appears that you can't change the importance of an already existing channel once you have created it.

Update: Turns out the workaround with NotificationManager.IMPORTANCE_MIN has a drawback.

When you use that importance level then your notification no longer displays fully inside the notification drawer, but inserts itself in a new Notification Channel Group which is collapsed by default (and will collapse itself again each time the drawer is pulled down). What a bummer!

Update 2: Digging a bit deeper it turns out that it is as if it correctly registered it as NotificationManager.IMPORTANCE_DEFAULT, but somehow it magically got upgraded to NotificationManager.IMPORTANCE_HIGH, like it would when the user explicitly changes the setting from default to high. That one also gets reset to default after turning the notification off and then on again.

like image 103
Daniel F Avatar answered Oct 19 '22 12:10

Daniel F


Well i will add a complete answer to help. If you read NotificationCompat code from androidx.

   /**
     * Silences this instance of the notification, regardless of the sounds or vibrations set
     * on the notification or notification channel.
     */
    public @NonNull Builder setNotificationSilent() {
        mSilent = true;
        return this;
    }

So you have to use like this if you want remove sound AND vibration.

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
            // no sound or vibration
            .setNotificationSilent()

If you want remove sound only. This is the way.

 // no sound
 builder.setSound(null);

If you want remove viration only.

 // no vibration
 mChannel.setVibrationPattern(new long[]{ 0 });
 mChannel.enableVibration(true);
like image 13
Zhar Avatar answered Oct 19 '22 14:10

Zhar