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.
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);
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.
You can set more than one channel. In my case, my aplication has a background service with two sounds notifications, and one notification without sound. I get it with this code:
//creating channels:
NotificationChannel channel1 = new NotificationChannel(CHANNEL_ID_1, "CarNotification1", NotificationManager.IMPORTANCE_HIGH);
NotificationChannel channel2 = new NotificationChannel(CHANNEL_ID_2, "CarNotification2", NotificationManager.IMPORTANCE_MIN);
NotificationChannel channel3 = new NotificationChannel(CHANNEL_ID_3, "CarNotification3", NotificationManager.IMPORTANCE_HIGH);
//mSound1 and mSound2 are Uri
channel1.setSound(mSound1, null);
channel3.setSound(mSound2, null);
and when I create the notification:
String channelId;
switch (situation){
case situation2:
channelId=CHANNEL_ID_2;
break;
case situation1:
channelId=CHANNEL_ID_1;
break;
default:
channelId=CHANNEL_ID_3;
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
//etcetera
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.
i had used this piece of code in NotificationCompat.Builder try this,
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setNotificationSilent();
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)