I want to show unread messages count on my android app's launcher icon. It should be cleared only once the user read all messages. I have tried setnumber() in NotificationCompat.Builder. But it is not showing the notification count.
I executed my code in two android devices.
Real Android device - version is 29 (red dot appeared in app launcher when i generate notification)
Android Emulator - version is 27 (nothing is shown)
Notification Channel Creation:
private void createNotificationChannel(String notificationChannelId, String systemTrayNotificationName){
Log.i("Tag","createNotificationChannel "+Build.VERSION.SDK_INT);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
CharSequence name = systemTrayNotificationName;
String description = systemTrayNotificationName;
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(notificationChannelId, name,importance);
notificationChannel.setDescription(description);
notificationChannel.setShowBadge(true);
notificationChannel.canShowBadge();
NotificationManager notificationManager = getContext().getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(notificationChannel);
}
}
Code to form notification
public void buildNotification(){
createNotificationChannel(NOTIFICATION_CHANNEL_ID,SYSTEM_TRAY_NOTIFICATION_NAME);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getContext(), NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_home_black_24dp)
.setLargeIcon(BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.ic_launcher))
.setContentTitle("Title")
.setContentText("Content Text")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setNumber(5)
.setBadgeIconType(NotificationCompat.BADGE_ICON_NONE)
.setAutoCancel(true);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(getContext());
// Issue the notification with notification manager.
notificationManager.notify(200, notificationBuilder.build());
}
The number of notifications must be supported by the launcher you are using.
As far as I know, the pixel launcher typical of google phones and present by default as launcher3 of the emulators does not support the notification count exactly as it happens in your first case.
You could try with a third party launcher to see if the counting works but as just said this feature must be natively supported by the launcher.
I am developing a stand alone app on Wear OS (Android 8+) and I have issues with notifications.
I am running a Foreground Service, with an on-going notification. That on-going notification works very well and has no feature from the Wear OS (so the code can work on standalone Android).
However, whenever I want to display other notifications, it is impossible.
No error message, nothing: my notifications are not displayed.
I made sure to create separate channels and to have them enabled (via the settings).
Here is my code, running in the Looper.mainLooper() via a Handler.
final Notification notification = new NotificationCompat.Builder(MonitorService.this, LOGS_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_backup_logs) // vector (doesn't work with png as well)
.setContentTitle(getString(R.string.monitor_service_notification_log_file_backed_up_process))
.setContentText("test")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.build();
notificationManagerCompat.notify(LOGS_ID, notification); // unique final static id
Am I missing something here ?
Thanks for the help !
Use this code
Notification.Builder b = new Notification.Builder(ctx);
//FIX android O bug Notification add setChannelId("shipnow-message")
NotificationChannel mChannel = null;
b.setSmallIcon(R.drawable.ic_backup_logs) // vector (doesn't work with png as well)
.setContentTitle(getString(R.string.monitor_service_notification_log_file_backed_up_process))
.setContentText("test")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mChannel = new NotificationChannel("your-channel", "yourSubjectName",NotificationManager.IMPORTANCE_HIGH);
b.setChannelId("your-channel");
}
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationManager.createNotificationChannel(mChannel);
}
notificationManager.notify(id, b.build());
I start a foreground service that shows up in the status bar under android systems battery information.
Is there a way to customize the information (title, subtext, icon, ...) presented?
service code: CODE EDITED
#Override
public void onCreate() {
context = this.getApplicationContext();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent notificationIntent = new Intent(context, CallbackTestWidgetService.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(context, 10, notificationIntent, 0);
Notification notification = new Notification.Builder(context, "Test")
.setContentTitle("Test")
.setContentText("text")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.test)
.setTicker("test")
.build();
CharSequence name = "test";
String description = "test";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("10", name, importance);
channel.setDescription("test");
// 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);
startForeground(10, notification);
}
}
What you're seeing is the default notification when no notification is posted by the app.
The reason why no notification is being posted (despite you calling startForeground) is that you are targeting API 26 or higher and not associating your notification with a notification channel. This causes your notification to be dropped entirely as per the note on that page:
Caution: If you target Android 8.0 (API level 26) and post a notification without specifying a notification channel, the notification does not appear and the system logs an error.
You must create a notification channel, then include the id of the notification channel when building your notification.
You're building a Notification to pass to startForeground. Use the createContentView function, just like you would for a normal notification you wanted to customize.
Note that this will only work on newer versions of Android, you probably want to use the NotiicationCompat class from the support library to support version specific api differences and test on a few different APIs of emulators.
Today I started targeting API 26 which forced me to use Notification Channels.
My problem is that now on each new notification (including updates to it) an annoying sound is played.
How can I disable this sound?
I tried replacing this sound with a custom mp3 sound in order to then pass it a mp3 with silence in it, but this is ignored.
I'm just adding a notification which is of very low priority, basically giving the user the option to perform some actions after he has interacted with the app. There's no reason to be loud, the user will know that he can refer to the notification because he has done a certain thing with the app which he knows that will cause a notification to appear.
The user will really start getting annoyed by that sound.
If you want to keep the importance of your channel and just remove the sound notificationChannel.setSound(null, null); seems to do the job.
EDIT:
Make sure to change the channel ID (and delete the old one) to have it applied to existing users. (Channels can be created, but never modified by the app, only the user can.)
(Update 2019-05: It gets worse with Android Q, there I'm even getting a sound when using NotificationManager.IMPORTANCE_LOW, at least in the Emulator...)
The solution is to use NotificationManager.IMPORTANCE_LOW and create a new channel for it. Once a channel is created, you can't change the importance (well, you can, but the new importance is ignored). The channel information appears to get stored permanently by the system and any channel created is only deleted when you uninstall the app. [Update: According to Ferran Negre's comment, you can delete the channel via nm.deleteNotificationChannel(nChannel.getId()); and recreate it with nm.createNotificationChannel(nChannel); but apparently there's a limitation that you can't create a channel with the same id of a deleted channel and expect to be able to apply different settings to the undeleted channel, see acoder's answer]
While previous Android versions played no sound back by default, this changed with Android O, but only when you target the API 26, that is, use Notification Channels. This is an inconsistency, well, actually, it's a bug:
The reason for this is that when you create a channel with NotificationManager.IMPORTANCE_DEFAULT (not soundworthy by default) Android will actually "somewhat" register it as NotificationManager.IMPORTANCE_HIGH (plays sound by default).
You can check this by going into the options of the notifications (long press on the notification entry), where you will get to read that it is of type NotificationManager.IMPORTANCE_HIGH and then disable the notification and then re-enable it. In this process it gets "downgraded" from the NotificationManager.IMPORTANCE_HIGH to the non-sounding, actually registered NotificationManager.IMPORTANCE_DEFAULT.
The bug has been submitted to the Android issue tracker, so you may want to star it (flagged by Google as "Won't Fix (Infeasible)", because... spoiled).
BTW, the new docs at https://developer.android.com/training/notify-user/channels
claim that the default behavior used to be that way, that default played a sound prior to Android 8.0, which is definitely not true. This is their list
User-visible importance level Importance Priority
(Android 8.0 and higher) (Android 7.1 and lower)
Urgent Makes a sound and appears as IMPORTANCE_HIGH PRIORITY_HIGH
a heads-up notification or PRIORITY_MAX
High Makes a sound IMPORTANCE_DEFAULT PRIORITY_DEFAULT
Medium No sound IMPORTANCE_LOW PRIORITY_LOW
Low No sound and does not appear IMPORTANCE_MIN PRIORITY_MIN
in the status bar
You can even see the mismatch between visibility importance high and notification importance high... I don't know why they are doing this. They definitely have a bug in their code.
Everything below the next line is obsolete, yet that bug mentioned there is still valid. My error there was to think that NotificationManager.IMPORTANCE_MIN is the next lower one from NotificationManager.IMPORTANCE_DEFAULT, but NotificationManager.IMPORTANCE_LOW is.
When you then go into the notification settings of the app via long-press-notification and all-channels button and toggle the switch for that channel off and on again, then it actually sets itself to NotificationManager.IMPORTANCE_DEFAULT and no sound will get played. I also noticed that after a crash it did get reset to NotificationManager.IMPORTANCE_HIGH
So basically the workaround is to use NotificationManager.IMPORTANCE_MIN. But you have to create a new channel so that this NotificationManager.IMPORTANCE_MIN is in effect, because it appears that you can't change the importance of an already existing channel once you have created it.
Update: Turns out the workaround with NotificationManager.IMPORTANCE_MIN has a drawback.
When you use that importance level then your notification no longer displays fully inside the notification drawer, but inserts itself in a new Notification Channel Group which is collapsed by default (and will collapse itself again each time the drawer is pulled down). What a bummer!
Update 2: Digging a bit deeper it turns out that it is as if it correctly registered it as NotificationManager.IMPORTANCE_DEFAULT, but somehow it magically got upgraded to NotificationManager.IMPORTANCE_HIGH, like it would when the user explicitly changes the setting from default to high. That one also gets reset to default after turning the notification off and then on again.
Well i will add a complete answer to help. If you read NotificationCompat code from androidx.
/**
* Silences this instance of the notification, regardless of the sounds or vibrations set
* on the notification or notification channel.
*/
public #NonNull Builder setNotificationSilent() {
mSilent = true;
return this;
}
So you have to use like this if you want remove sound AND vibration.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
// no sound or vibration
.setNotificationSilent()
If you want remove sound only. This is the way.
// no sound
builder.setSound(null);
If you want remove viration only.
// no vibration
mChannel.setVibrationPattern(new long[]{ 0 });
mChannel.enableVibration(true);
NotificationCompat.Builder.setSilent(true)
This allows you to post a silent notification (no sound or vibration) regardless of the channel's importance setting.
Reference:
https://developer.android.com/reference/androidx/core/app/NotificationCompat.Builder#setSilent(boolean)
As far as I have seen, since API 26 (Oreo) it is not possible to change the sound of a notification after it was once created.
notificationManager.deleteNotificationChannel("channel_id"));
NotificationChannel notificationChannel = new NotificationChannel(
"channel_id", "channel_name",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setSound(null, null);
notificationManager.createNotificationChannel(notificationChannel);
Even deleting the channel before creation does not help.
Google documentation says:
android.app.NotificationManager public void deleteNotificationChannel(String channelId)
Deletes the given notification channel.
If you create a new channel with this same id, the deleted channel will be un-deleted with all of the same settings it had before it was deleted.
NotificationChannel#setSound() documentation states
Only modifiable before the channel is submitted to NotificationManager#createNotificationChannel(NotificationChannel)
Too bad that notificationBuilder.setSound(defaultSoundUri) does not work as well:
This method was deprecated in API level 26. Use NotificationChannel#setSound(Uri, AudioAttributes) instead.
Also using support library does not work. So sound is only settable once in the app and changing by the user is only possible in the settings of the notification. For me Ferran Negre's comment did not work. I do not understand why Google made this restriction. Too bad.
I have tested a lot of android devices,the following code works for me properly
Firstly, create a notificationBuilder, if your Build.Version is bigger than 26, please add a new channel.
private val notificationBuilder: NotificationCompat.Builder by lazy {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) NotificationCompat.Builder(context) else {
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channelId = "MUSIC"
val channelName = "音乐控制栏"
val importance = NotificationManager.IMPORTANCE_MIN
val channel = NotificationChannel(channelId, channelName, importance)
manager.createNotificationChannel(channel)
channel.enableLights(false)
channel.vibrationPattern = longArrayOf(0L)
channel.enableVibration(false)
channel.setSound(null, null)
NotificationCompat.Builder(context, channelId)
}
}
Secondly, init this notificationBuilder, and set sound null
notificationBuilder.setDefaults(Notification.DEFAULT_LIGHTS ).setVibrate( longArrayOf(0L)).setSound(null)
Thirdly,if build.version is bigger than 24, please set its priority.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
notificationBuilder.priority = NotificationManager.IMPORTANCE_MIN
}
Hope that works for you.
NotificationManager.IMPORTANCE_LOW
It produces no sound when notification is created as I need the same in my Music Application.
And yes if you have already created a notification channel then either you need to change the channel id or simply uninstall the previous application and install again.
For me the solution was to create group notification.
val builder = NotificationCompat.Builder(this)
.setGroupAlertBehavior(GROUP_ALERT_SUMMARY)
.setGroup("My Group")
.setGroupSummary(false)
.setDefaults(DEFAULT_ALL)
.setSound(null)
But in this case, if you send a new notification with a new ID, then it will be grouped with the previous notifications.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(
channelId.toString(), title,
NotificationManager.IMPORTANCE_DEFAULT
)
notificationChannel.setSound(null,null)
notificationChannel.enableVibration(false)
notificationChannel.description = body
if(notificationManager.getNotificationChannel(channelId.toString())==null) {
notificationManager.createNotificationChannel(notificationChannel)
}
if (data["sound"]?.equals("default", true) == true) {//if your app need contorl sound enable
RingtoneManager.getRingtone(
this,
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
).play()
}
if(pushShake.isTrue() ){//if your app need contorl vibarate enable
val vbmanager= getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vbmanager.vibrate(VibrationEffect.createOneShot(500,VibrationEffect.DEFAULT_AMPLITUDE))
}
}
below code is about notification, but sound,vibrate will not play at API 26 ,so dont worry about setsound or setvibrate
notificationManager.notify(channelId.toInt(), notificationBuilder.apply {
setContentIntent(pendingIntent)
setSmallIcon(R.drawable.img_logo)
setTicker(title)
setNumber(data["badge"]?.toIntOrNull() ?: 0)
setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
color = if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
resources.getColorMuteDepre(R.color.colorAccent2)
} else {
Color.parseColor("#ffffff")
}
setContentTitle(title)
setContentText(body)
setWhen(System.currentTimeMillis())
setAutoCancel(true)
setSound(null)
setVibrate(longArrayOf())
if (pushShake.isTrue() && data["sound"]?.equals("default", true) == true) {
setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
val vbmanager = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vbmanager.vibrate(500)
}
}else{
if (data["sound"]?.equals("default", true) == true) {
setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
}
if (pushShake.isTrue() ) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
val vbmanager = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vbmanager.vibrate(500)
}
}
}
setStyle(
NotificationCompat.BigTextStyle().bigText(body).setSummaryText(body).setBigContentTitle(
title
)
)
setPriority(NotificationCompat.PRIORITY_DEFAULT)
}.build())
As IMPORTANCE solution has the side effect of no notification popup, I got a final solution is:
adding a silent sound resource which downloaded from below repo
set sound for the channel with the silent sound resource.
https://github.com/anars/blank-audio/blob/master/1-second-of-silence.mp3
If the case is like mine, that I am forced to show a notification for background service and I don't really want to show any notification the solution that worked on 8.0 was:
.setPriority(NotificationManager.IMPORTANCE_NONE)
With this not only I didn't get the annoying sound every 5 minutes but also minimized the appearance of the notification itself.
On 8.1 I didn't have the problem with the sound with following:
.setPriority(NotificationManager.IMPORTANCE_MIN)
You can use 2 different notification channel to send notification depending on there priority to user.
If its a high priority notification the send it via
new NotificationChannel("Channel ID", "Channel Name", NotificationManager.IMPORTANCE_HIGH);
Your user will get sound and pop when they will receive a notification.
If you want to send less important notification then use this channel.
new NotificationChannel("Channel ID", "Channel Name", NotificationManager.IMPORTANCE_LOW);
Your user will get a notification with no sound and pop up.
check different priority from here - https://developer.android.com/reference/android/app/NotificationManager
No need to use .setSound(null, null)
just use below code
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,
getString(R.string.notification_channel_id))
.setContentTitle("Audio Recording")
.setContentText("recording in progress")
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setSmallIcon(R.mipmap.ic_launcher);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
getString(R.string.notification_channel_id), "AOD+", NotificationManager.IMPORTANCE_LOW
);
channel.setDescription("Call Recorder is running");
channel.setShowBadge(true);
channel.canShowBadge();
channel.setLightColor(Color.GREEN);
channel.enableVibration(false);
assert notificationManager != null;
notificationManager.createNotificationChannel(channel);
}
assert notificationManager != null;
startForeground(256/* must be greater than 0*/, notificationBuilder.build()); //I am using this for audio recording
//notificationManager.notify(0, notificationBuilder.build());
First you need to set
importance = NotificationManager.IMPORTANCE_LOW;
then
Notification n = builder.setPriority(Notification.DEFAULT_ALL).build();
n.defaults = 0;
n.defaults |= Notification.DEFAULT_VIBRATE;
NotificationManager.IMPORTANCE_LOW
This will work but also collapsed your notification.
NotificationManager.IMPORTANCE_LOW will work for sure
I faced the same issue in the 8.1 and made the below changes. Change the priority of the notification as LOW and it will disable the sound of notification.
NotificationManager.IMPORTANCE_LOW
The code is like following:
NotificationChannel chan1 = new NotificationChannel("default", "default", NotificationManager.IMPORTANCE_LOW);
With the below code my notification are only added to the notification bar, no popup style message is displayed like if you would receive a whatsapp message when you're in another application. What makes that happen to a notification?
private void sendNotification(int distance, ViewObject viewObject) {
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("path", viewObject.getPath());
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(notificationIntent);
PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(Integer.parseInt(viewObject.getRefId()), PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
bigText.bigText(String.format(getString(R.string.notification), viewObject.getTitle()));
bigText.setBigContentTitle(getString(R.string.hello));
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_wald_poi)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_poi))
.setColor(getResources().getColor(R.color.primary))
.setContentTitle(getString(R.string.hello))
.setContentIntent(notificationPendingIntent)
.setContentText(String.format(getString(R.string.notification), viewObject.getTitle()))
.setDefaults(Notification.DEFAULT_ALL)
.setStyle(bigText);
builder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());
}
If you want use Heads-up Notifications like this:
You must change Notification priority or NotificationChannel importance.
The notification priority, set by setPriority(). The priority determines how intrusive the notification should be on Android 7.1 and lower. (For Android 8.0 and higher, you must instead set the channel importance)
On Android 7.1 (API level 25) and lower:
Set notification priority to NotificationCompat.PRIORITY_HIGH or NotificationCompat.PRIORITY_MAX.
Set ringtone and vibrations - you can use setDefaults(Notification.DEFAULT_ALL)
Android 8.0 (API level 26) and higher:
Set notification channel priority to NotificationManager.IMPORTANCE_HIGH
Notification:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_wald_poi)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_poi))
.setColor(getResources().getColor(R.color.primary))
.setContentTitle(getString(R.string.hello))
.setContentIntent(notificationPendingIntent)
.setContentText(String.format(getString(R.string.notification), viewObject.getTitle()))
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setStyle(bigText)
.setPriority(NotificationCompat.PRIORITY_HIGH) // or NotificationCompat.PRIORITY_MAX
Notification channel:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create the NotificationChannel
val name = getString(R.string.notification_channel_name)
val descriptionText = getString(R.string.notification_channel_description)
val importance = NotificationManager.IMPORTANCE_HIGH
val mChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance)
mChannel.description = descriptionText
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(mChannel)
}
Important
If you'd like to further customize your channel's default notification behaviors, you can call methods such as enableLights(), setLightColor(), and setVibrationPattern() on the NotificationChannel. But remember that once you create the channel, you cannot change these settings and the user has final control of whether these behaviors are active. Other option is to uninstall and install application again.
Read more
Examples of conditions that may trigger heads-up notifications include:
The user's activity is in fullscreen mode (the app uses
fullScreenIntent).
The notification has high priority and uses
ringtones or vibrations on devices running Android 7.1 (API level 25)
and lower.
The notification channel has high importance on devices
running Android 8.0 (API level 26) and higher.
Priority:
Notification.PRIORITY_HIGH and Notification.PRIORITY_MAX was deprecated in API level 26. use NotificationCompat instead.
Here is more info :-)
You have to set the notification priority to Notification.PRIORITY_HIGH or Notification.PRIORITY_MAX. I had to also do .setDefaults(Notification.DEFAULT_ALL).
Below API level 26:
Set Notification.PRIORITY_HIGH or Notification.PRIORITY_MAX in the Builder when sending the message.
API level 26 or above:
Set channel priority high
Important:
Once the channel has a established priority it cannot be changed. If it was in LOW will not show the popup, unless you reinstall the APP.
The Android system is deciding wheter a notification is displayed as Heads-up notification. There are several conditions which may trigger a Heads-up notification:
Examples of conditions that may trigger heads-up notifications include:
The user's activity is in fullscreen mode (the app uses fullScreenIntent).
The notification has high priority and uses ringtones or vibrations on devices running Android 7.1 (API level 25) and lower.
The notification channel has high importance on devices running Android 8.0 (API level 26) and higher.
Source: https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Heads-up
So if you're running Android 7.1 and lower, be sure to add a ringtone or vibration as well as the high priority. For Android 8.0 and higher, change the priority to high importance.
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
// ...
.setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE)
.setPriority(NotificationCompat.PRIORITY_HIGH); // or HIGH_IMPORTANCE for Android 8.0