Android disable notification sounds - android

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();

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();

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

In Android 26+ (Oreo) you do this by setting notification channel importance level to 'IMPORTANCE_LOW' or below.
val importance = NotificationManager.IMPORTANCE_LOW
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
description = descriptionText
}

Related

NotificationManager.IMPORTANCE_HIGH still no sound

I am trying to create notifications in android but no matter what importance and priority I set, there's no sound. I am assuming sound (ringtone) is handle by android itself I don't need to provide any mp3/wav file. I am trying on android 8.1 (actual device), 8.0 (emulator) and 8.1 (emulator). Notification channel created on actual device has sound off by default, I don't know why and on emulator sound is on but still no sound played on notification
Here is my code:
public void sendMessage(View view) {
createNotificationChannel();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("New Notification")
.setContentText("Lorem Ipsum")
.setPriority(NotificationCompat.PRIORITY_HIGH);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1, builder.build());
}
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "basic-channel";
String description = "Lorem Ipsum";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
Channel on Actual Device
Channel on Emulator
Root Cause:
For actual device, that's OEM's problem, which in my case is Xiaomi and I found this link threema.ch/en/faq/notification_channels_xiaomi which says that xiaomi sets sound=off for all app except select few like FB, whatsApp, etc.
For emulator, we need to complete the setup process after which notification starts making sound.
When you build the notification, you need to indicate that you want to use the default system values:
builder.setDefaults(Notification.DEFAULT_ALL)
And remember to clear the app data or reinstall the app to properly recreate the notification channel. A notification channel will retain its initial configuration even if you recreate it in code.
Check this out :
//Define sound URI
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("New Notification")
.setContentText("Lorem Ipsum")
.setPriority(NotificationCompat.PRIORITY_HIGH);
.setSound(soundUri); //This sets the sound to play

Disable swipe removal notification in Android Oreo [duplicate]

This question already has answers here:
non removable notification
(4 answers)
Closed 4 years ago.
I am working on music application so i need to display notification on notification bar. for that i am using custom notification. The following is the code for notification for Android Oreo
String NOTIFICATION_CHANNEL_ID = "001";
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.mynotification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_LOW);
notificationChannel.setDescription("Channel description");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MainActivity.this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setSmallIcon(R.drawable.exo_edit_mode_logo)
.setCustomContentView(notificationLayout)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_DEFAULT);
NotificationManagerCompat com = NotificationManagerCompat.from(this);
com.notify(001, notificationBuilder.build());
}
This code is working fine, but when swipe the notification from notification bar it will be removed. But my requirement is not remove when swipe the notification.
I tried with setAutoCancel(true) and setAutoCancel(false). But not working it will be removed. So how to stick the notification here.
Please guide me how to do this.
Thanks In Advance
use setOngoing(true) to achieve this
You have to add this setOngoing(true)
Try:
Notification.Builder builder = new Notification.Builder(this, CHANNEL_ID)
.setContentTitle(getString(R.string.app_name))
.setContentText("Text")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(false)
.setOngoing(true);
Notification notification = builder.build();

How to create Notification.contentView in android Nougat and above?

I use Notification.contentView to replicate notification views:
View notificationView = notification.contentView.apply(context, parent);
Unfortunately as of version N, Notification.contentView may be null and deprecated, so how can I create Notification.contentView manually?
Generally I create notification this way:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
builder.setAutoCancel(true)
.setColor(ContextCompat.getColor(this, R.color.colorAccent))
.setContentTitle(title)
.setContentText(text)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(when)
.setSmallIcon(smallIcon);
Then if I create the contentView manually, what can I do to map all the above settings?
Important note: I don't call setCustomContentView, I want to reproduce a contentView for the standard notification.
Notification.contentView()
YES From android N, this field may be null
The notification view is determined by the inputs to Notification.Builder; a custom RemoteViews can optionally be supplied with Notification.Builder.setCustomContentView(RemoteViews)
SAMPLE CODE
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "Nilesh_channel";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("");
notificationChannel.enableLights(true);
notificationChannel.enableVibration(true);
mNotificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.custom_layout);
notificationBuilder.setAutoCancel(true)
.setColor(ContextCompat.getColor(this, R.color.colorAccent))
.setContentTitle(getString(R.string.app_name))
.setContentText("")
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher_background)
.setCustomContentView(notificationView) // set here your RemoteViews
.setAutoCancel(true);
OUTPUT
Answer my own question:
Create contentView by Notification.Builder:
builder.createContentView();
Create contentView by Notification:
Notification.Builder.recoverBuilder(context, notification).createContentView();
Since Notification.Builder.createContentView() was introduced in api level 24, so the above code can only be called from Nougat 7.0 or newer devices; For a lower version phones, it's always safe to reference non-null Notification.contentView directly, it's created automatically by the android system after builder.build() was called.

Android colorful notification pictures

I want to implement colorful, customized notification in my Android App.
I tried to implement color, but for the moment I can only set the background color. I want to reach this kind of result:
With a image of my app shown as a notification pic when expanded.
Here my code at the moment:
return builder.setContentTitle("APP TITLE")
.setContentText("Some texts")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.setOngoing(true)
.setAutoCancel(true)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setPriority(Notification.PRIORITY_HIGH)
.setCategory(Notification.CATEGORY_ALARM)
.setOnlyAlertOnce(false)
.setColor(getResources().getColor(R.color.colorPrimary));
How can I implement the icon notification colorful, as the hangouts example I posted above?
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText(messs)
.setLargeIcon(bitmap)
.setSmallIcon(R.mipmap.hangouts_launcher)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
mNotifyBuilder.setContentIntent(pIntent);
// Set Vibrate, Sound and Light           
int defaults = 0;
defaults = defaults | Notification.DEFAULT_LIGHTS;
defaults = defaults | Notification.DEFAULT_VIBRATE;
defaults = defaults | Notification.DEFAULT_SOUND;
mNotifyBuilder.setDefaults(defaults);
// Set the content for Notification
mNotifyBuilder.setContentText(messs);
mNotificationManager.notify(m, mNotifyBuilder.build());

setgroup() in notification not working

I'm tring to create notification group, this is my code:
// Build the notification, setting the group appropriately
Notification notif = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("New mail from " + 1)
.setContentText("cv")
.setSmallIcon(R.drawable.rh_logo)
.setStyle(new NotificationCompat.InboxStyle()
.addLine("Alex Faaborg Check this out")
.addLine("Jeff Chang Launch Party")
.setBigContentTitle("2 new messages")
.setSummaryText("johndoe#gmail.com"))
.setGroup(GROUP_KEY_EMAILS)
.setGroupSummary(true)
.build();
// Issue the notification
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(++NOTIFICATION_ID, notif);
When I run the app, and send notification messages, they do not show in group.
Can someone explain me what I need to change?
You have to create a group notification before creating your custom notification. Just like this:
NotificationCompat.Builder groupBuilder =
new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(content)
.setGroupSummary(true)
.setGroup("GROUP_1")
.setStyle(new NotificationCompat.BigTextStyle().bigText(content))
.setContentIntent(pendingIntent);
Do not forget setGroupSummary to true.
Then create your child notification which group value is same to groupBuilder's value。Here is "GROUP_1".
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_communication_invert_colors_on)
.setContentTitle(title)
.setContentText(content)
.setGroup("GROUP_1")
.setStyle(new NotificationCompat.BigTextStyle().bigText(content))
.setContentIntent(pendingIntent)
Finally use NoticationManagerCompat to notify them.
NotificationManagerCompat manager = NotificationManagerCompat.from(context);
manager.notify(GROUP_ID, groupBuilder.build());
manager.notify(id, builder.build());
Replace
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(++NOTIFICATION_ID, notif);
with
NotificationManagerCompat.from(mCtx).notify(++NOTIFICATION_ID,notif);
because you are using NotificationCompat
There is a strange behavior of notification. If you add autocancel notification group doesn't work, if you add setGroup or setGroupSummary. It will not work. Removing all that fixed the problem for me. Basically, you don't need to mention any group it will auto group. TESTED on Redmi 10 pr0, Poco x3 NFC, huawei device.
return new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_action_name)
.setContentTitle(title)
.setContentText(body)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setPriority(NotificationCompat.PRIORITY_LOW);

Categories

Resources