Android heads-up notification not showing

Viewed 19903

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.

6 Answers

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

I've encountered the same issue. The solution is simply properly setting the vibration builder.setVibration(NotificationCompat.DEFAULT_VIBRATE).

According to Google:

The notification has high priority and uses ringtones or vibrations on devices running Android 7.1 (API level 25) and lower.

I hope it helps :)

The answers provided above were correct, but are partially correct now.

For future readers, notice that

    .setDefaults(Notification.DEFAULT_ALL)
    .setPriority(Notification.PRIORITY_HIGH)

will NOT resolve the problem.

According to "Android for developers", https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Heads-up

Example conditions that might trigger heads-up notifications include the following:

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

Notice that the second solution (which is mostly mentioned under this question), only works for Android 7.1 or lower.

For Android 8.0 and higher, you should create a notification channel whose importance is NotificationManager.IMPORTANCE_HIGH.

Some sample code:

    NotificationChannel channel =
        new NotificationChannel(
        "MY_OWN_CHANNEL_ID",
        "MY_OWN_CHANNEL_NAME",
        NotificationManager.IMPORTANCE_HIGH);

and after that, make sure that your notification builder uses this channel to display

    NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(context)
            .setSmallIcon(smallIcon)
            .setContentTitle("Title")
            .setContentText("Content")
            .setChannelId("MY_OWN_CHANNEL_ID")
            .setPriority(Notification.PRIORITY_HIGH)
            .setVibrate(new long[0]);

Now you should be able to see heads-up notifications properly.

Related