25

I have a notification with this code :

Notification notifica = new Notification();
notifica.flags |= Notification.FLAG_AUTO_CANCEL;
notifica.icon = R.drawable.serie_notification;
notifica.when = System.currentTimeMillis();

with notifica.defaults = notifica.defaults|Notification.DEFAULT_SOUND; I enable the default sound but if I want to disable the sound how can I do ??

MimmoG
  • 631
  • 3
  • 14
  • 25
  • I thought it was disabled by default. I don't remember it making a sound on the notification I made... – Bob Oct 05 '11 at 00:03
  • There's a bug in the Notification Channels when targeting API 26 (Android O) https://stackoverflow.com/questions/45919392/disable-sound-from-notificationchannel – Daniel F Aug 28 '17 at 14:26
  • Does this answer your question? [How to show a notification without a sound java](https://stackoverflow.com/questions/39809702/how-to-show-a-notification-without-a-sound-java) – Marvin Apr 21 '23 at 15:15

7 Answers7

20

Well, it worked for me by doing this:

myNotification.defaults = 0;

Try it out =)

Ted
  • 19,727
  • 35
  • 96
  • 154
12

To show notifications without sound on pre Oreo devices, Oreo and above devices



    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent intent = new Intent(this, AlertDetails.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

    String CHANNEL_ID = "channel_id";

    // You must create the channel to show the notification on Android 8.0 and higher versions
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // Set importance to IMPORTANCE_LOW to mute notification sound on Android 8.0 and above
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "name", NotificationManager.IMPORTANCE_LOW);
        notificationManager.createNotificationChannel(channel);
    }

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("My notification")
            .setContentText("Hello World!")
            // You must set the priority to support Android 7.1 and lower
            .setPriority(NotificationCompat.PRIORITY_LOW) // Set priority to PRIORITY_LOW to mute notification sound 
            .setContentIntent(pendingIntent)
            .setAutoCancel(true); 

    notificationManager.notify(
                    1001, // notification id
                    mBuilder.build());

Sridhar Nalam
  • 527
  • 8
  • 9
7

It is possible to do that basically just enable all of the other Notification.defaults except the sound ( which is Notification.DEFAULT_SOUND ).

Here is an example that will work for you:

myNotification.defaults = 0;
myNotification.defaults |= Notification.DEFAULT_VIBRATE;

Here are all of the available the options you can select:

Notification.DEFAULT_LIGHTS
Notification.DEFAULT_VIBRATE
Notification.DEFAULT_SOUND
Notification.DEFAULT_ALL // This enables all above 3

Update

Notification .defaults deprecated

aleksandrbel
  • 1,422
  • 3
  • 20
  • 38
Aki
  • 3,709
  • 2
  • 29
  • 37
5

In newer versions of Android, u have to set the prioirity of notification channel to low:

var channel = new NotificationChannel(notificationChannelName, "channel", NotificationManager.IMPORTANCE_LOW);
Arutyun Enfendzhyan
  • 1,612
  • 1
  • 12
  • 15
4

NotificationCompat.Builder method

setSilent(true)
Quentin
  • 51
  • 1
  • 4
2

android "O" >= , Disable or Enable sound of the notification you use two ids for notification channel. one for sounded notification and another is set when you want to disable

@RequiresApi(api = Build.VERSION_CODES.O)
    private void createNotificationChannel(Context context, NotificationManager mNotificationManager, boolean playSound) {

    // The user-visible name of the channel.
    CharSequence name = context.getString(R.string.channel_name);

    // The user-visible description of the channel.

    String description = context.getString(R.string.channel_description);
    int importance = NotificationManager.IMPORTANCE_HIGH;
    NotificationChannel mChannel = new NotificationChannel(playSound ? channel_id_sound : channel_id_no_sound, name, importance);

    // Configure the notification channel.
    mChannel.setDescription(description);
    mChannel.enableLights(true);

    // Sets the notification light color for notifications posted to this
    // channel, if the device supports this feature.
    mChannel.setLightColor(Color.RED);
    mChannel.enableVibration(true);
    mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
    if (!playSound)
        mChannel.setSound(null, null);
    mNotificationManager.createNotificationChannel(mChannel);
}

Satish Ravani
  • 121
  • 1
  • 7
1

You can disable the sound using this

NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Channel name", NotificationManager.IMPORTANCE_LOW);
channel.setSound(null, null);

Just set the sound of channel as null

Manish Arora
  • 397
  • 3
  • 12