I am trying to make heads-up notification work. Notification is created, but it's not displayed at the top of the app. Here's the code responsible for building a notification:
Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.arrow_up_float)
.setContentTitle("Check running time - click!")
.setContentText(String.valueOf(elapsedTime))
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
.setVibrate(new long[0])
.build();
The device that I'm trying to run the app is API 21. I've seen many threads, but no solution given works for me.
Do Not Disturb or Airplane Mode is on. Either system or app notifications are disabled. Power or data settings are preventing apps from retrieving notification alerts. Outdated apps or OS software can cause apps to freeze or crash and not deliver notifications.
Check Notification Settings Your first course of action is to check the notification settings on your phone to ensure they aren't disabled. Step 1: Launch the Settings app and go to Notifications. Step 2: Tap on Included apps and ensure that switch next to All apps is enabled.
You need to do two things:
Make sure that your notification is properly configured. See: https://developer.android.com/guide/topics/ui/notifiers/notifications#Heads-up
AND make sure the phone is properly configured (I think this is where most get stuck).
Step 1. Configure the notification.
First, register your notification channel like so
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH; //Important for heads-up notification
NotificationChannel channel = new NotificationChannel("1", name, importance);
channel.setDescription(description);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
Then, create a notification, like so:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "1")
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(textTitle)
.setContentText(textContent)
.setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE) //Important for heads-up notification
.setPriority(Notification.PRIORITY_MAX); //Important for heads-up notification
Finally, send the notifciations as you would do normally, e.g.:
Notification buildNotification = mBuilder.build();
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(001, buildNotification);
Step 2. Configure the phone.
I noticed that I have to enable some additional settings on my phone (a Xiaomi Note 3):
Here are some ways to reach the menu:
startActivity(new Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS));
Finally, when you reach this menu enable a setting called something like "Floating notification" (the name of this setting varies between devices).
your code is almost fine. I'm using DEFAULT_VIBRATE instead of DEFAULT_ALL:
builder.setPriority(Notification.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= 21) {
mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
}
But, you can also set
builder.fullScreenIntent(sameAsContentPendingtIntent);
but ^ this one is non-deterministic. I mean that system choose to display a HeadsUp or launch Intent. In most cases it shows HeadUp, but I'm not counting on it because of Android versions, Manufacturers, launchers and so so on.
Not at last, you also need to define right Notification Channel. I think you had already done this, because you wouldn't see any notification if you don't :-) Oh lovely Android :-) Anyway, I want to say that also NotificationChannel needs to be in high priority:
channel = new NotificationChannel("uniqueId", "name", NotificationManager.IMPORTANCE_HIGH);
channel.enableVibration(true);
And also, I advise to you, check latest opinions at developer.android.com
Google is going to be more strict from now. Not only for notifications but also for 'targetApi', but that is another story, pardon me :-)
Have a nice code today
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With