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条回答
甜甜的少女心
2楼-- · 2019-06-15 07:32

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);
查看更多
贪生不怕死
3楼-- · 2019-06-15 07:34

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

查看更多
Viruses.
4楼-- · 2019-06-15 07:34

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

查看更多
\"骚年 ilove
5楼-- · 2019-06-15 07:37

use that exact code:

NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_DEFAULT);

NOTIFICATION_CHANNEL_ID --> random String.
channelName ==> random string
查看更多
该账号已被封号
6楼-- · 2019-06-15 07:40

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.

查看更多
小情绪 Triste *
7楼-- · 2019-06-15 07:44

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);
查看更多
登录 后发表回答