Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android disable notification sounds

I want to create a notification without any sounds. How can I do this? I tried the code below but it's not working for me:

notification = mBuilder
        .setStyle(notiStyle)
        .setSmallIcon(notificationIcon)
        .setTicker(title)
        .setWhen(0)
        .setAutoCancel(true)
        .setContentTitle(title)
        .setContentIntent(resultPendingIntent)
        .setSound(null).build();
like image 256
Mehmet Demir Avatar asked Dec 25 '22 08:12

Mehmet Demir


2 Answers

You can create the NotificationCompat.Builder object without using setSound(). This will create a notification without any sound.

notification = mBuilder
                .setStyle(notiStyle)
                .setSmallIcon(notificationIcon)
                .setTicker(title)
                .setWhen(0)
                .setAutoCancel(true)
                .setContentTitle(title)
                .setContentIntent(resultPendingIntent)
                .build();
like image 80
Shadab Ansari Avatar answered Jan 05 '23 18:01

Shadab Ansari


After your notification builder, add

notification.defaults = 0;

to tell the notification manager not to make any default values when not specified (eg. you set to null so it takes the default value, adding this will remove this behavior and so disable sounds completely).

like image 45
Chaoz Avatar answered Jan 05 '23 17:01

Chaoz