Android heads-up notification not showing

You need to do two things:

  1. Make sure that your notification is properly configured. See: https://developer.android.com/guide/topics/ui/notifiers/notifications#Heads-up

  2. 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:

  1. Long press a notification, in the notification bar.
  2. Go to: Settings > Installed apps > Select your app > Notifications
  3. Improving on the previous step, you can help users partially by sending them to the installed apps menu, by using this intent:

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