Heads-up Notification - Android Lollipop

54,588

Solution 1

According to Notifications, you are required to set a vibrate or ringtone to make Heads-up work. However, here's a quick hack that doesn't require VIBRATE permission to produce a head-up notification:

notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= 21) notificationBuilder.setVibrate(new long[0]);

EDIT:

Don't abuse heads-up notification. See here for when to use heads-up notification:

MAX: For critical and urgent notifications that alert the user to a condition that is time-critical or needs to be resolved before they can continue with a particular task.

HIGH: Primarily for important communication, such as messages or chat events with content that is particularly interesting for the user. High-priority notifications trigger the heads-up notification display.

Solution 2

According to Google: https://developer.android.com/design/patterns/notifications.html

If a notification's priority is flagged as High, Max, or full-screen, it gets a heads-up notification.

So the following code should generate an heads-up notification:

.setPriority(Notification.PRIORITY_MAX)

Should be enough. But apparently the .setDefaults(Notification.DEFAULT_VIBRATE) has to be set also. Hopefully Google will fix this in their final release of Android 5.0.

Not sure if bug or feature...

Solution 3

All my apps doesn´t show the Notification, for example i have a Nexus 6 with Android 5.1.1, but i think this is an issuse since Android 5.0, i had to set:

.setPriority(Notification.PRIORITY_HIGH)

Correctly set and manage notification priority

Android supports a priority flag for notifications. This flag allows you to influence where your notification appears, relative to other notifications, and helps ensure that users always see their most important notifications first. You can choose from the following priority levels when posting a notification:

MAX Use for critical and urgent notifications that alert the user to a condition that is time-critical or needs to be resolved before they can continue with a particular task.

HIGH Use primarily for important communication, such as message or chat events with content that is particularly interesting for the user. High-priority notifications trigger the heads-up notification display.

DEFAULT Use for all notifications that don't fall into any of the other priorities described here and if the application does not prioritize its own notifications

LOW Use for notifications that you want the user to be informed about, but that are less urgent. Low-priority notifications tend to show up at the bottom of the list, which makes them a good choice for things like public or undirected social updates: The user has asked to be notified about them, but these notifications should never take precedence over urgent or direct communication.

MIN Use for contextual or background information such as weather information or contextual location information. Minimum-priority notifications do not appear in the status bar. The user discovers them on expanding the notification shade.

Solution 4

To set the priority, use the setPriority function (introduced in API 16) alongwith setDefaults (added in API 11) of Notification Builder. Choose the priority DEFAULT, HIGH, LOW, MAX, MIN as per the requirement of your app. Defaults can also be chosen here.

A small snippet:

notification = NotificationBuilder(service)
notification.setPriority(Notification.PRIORITY_MAX)
notification.setDefaults(Notification.DEFAULT_ALL)

Solution 5

Please check that your phone is not in “silent” or “do not disturb” mode. I spent day before I found it. I just leave this comment for those who get the same problem and found this question.

Share:
54,588
user1719863
Author by

user1719863

Updated on July 17, 2022

Comments

  • user1719863
    user1719863 almost 2 years

    I'm trying to show a notification-type heads-up but I could not. What I tried

    final Notification.Builder notif = new Builder(getApplicationContext())
        .setContentTitle(getString(R.string.title))
        .setContentText(getString(R.string.text))
    //  .setTicker(getString(R.string.tick)) removed, seems to not show at all
    //  .setWhen(System.currentTimeMillis()) removed, match default
    //  .setContentIntent(contentIntent) removed, I don't neet it
        .setColor(Color.parseColor(getString(R.color.yellow))) //ok
        .setSmallIcon(R.drawable.ic_small) //ok
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
    //  .setCategory(Notification.CATEGORY_CALL) does not seem to make a difference
        .setPriority(Notification.PRIORITY_MAX); //does not seem to make a difference
    //  .setVisibility(Notification.VISIBILITY_PRIVATE); //does not seem to make a difference
    
    mNotificationManager.notify(Constants.NOTIFICATION_ID, notif.build());
    

    The notification is shown only as an icon in the bar. I'm using API 21 on API 21 emulator (not L preview) I have tried:
    android:Theme.Holo.NoActionBar,
    android:Theme.Holo.NoActionBar.Fullscreen
    and NotificationCompat.Builder

    SDK examples are not available. does anyone know how to do it?

    I made it working by adding:

    .setDefaults(Notification.DEFAULT_VIBRATE)
    

    is this the best way?