How to show a notification without a sound java

2019-06-15 07:03发布

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.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

8条回答
ら.Afraid
2楼-- · 2019-06-15 07:46

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)
查看更多
▲ chillily
3楼-- · 2019-06-15 07:49

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.

查看更多
登录 后发表回答