Android: How can I put my notification on top of notification area?

41,046

Solution 1

You should do this. Other answers seem outdated.

NotificationCompat.Builder mBuilder =
            (NotificationCompat.Builder) new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.some_small_icon)
            .setContentTitle("Title")
            .setContentText("This is a test notification with MAX priority")
            .setPriority(Notification.PRIORITY_MAX);

setPriority(Notification.PRIORITY_MAX) is important. It can also be replaced with any of the following as per requirement.

Different Priority Levels Info:

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

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

PRIORITY_DEFAULT -- Use for all notifications that don't fall into any of the other priorities described here.

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

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

For more details check the following link: http://developer.android.com/design/patterns/notifications.html#correctly_set_and_manage_notification_priority

Solution 2

You can make your notification Ongoing, when it will appear higher then other usual notification. But in this case user would not be able to clear it manually.

In order to do this set flags to your Notification object:

notif.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR

Solution 3

Try setting priority of the notification to high

documentation > Notification Priority

Also check this question may it could help you Pin Notification to top of notification area

Solution 4

Please note that if you want a "heads-up" notification i.e., one that displays over the top of the current user window you must have the following set in your builder:

setDefaults(NotificationCompat.DEFAULT_VIBRATE)

The reference is in the javadoc:

A notification that vibrates is more likely to be presented as a heads-up notification, on some platforms.

Complete example for a heads-up notification:

NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.some_small_icon)
            .setContentTitle("Title")
            .setContentText("This is a test notification with MAX priority")
            .setPriority(Notification.PRIORITY_MAX)
            .setDefaults(NotificationCompat.DEFAULT_VIBRATE);
Share:
41,046

Related videos on Youtube

Meroelyth
Author by

Meroelyth

Updated on December 09, 2021

Comments

  • Meroelyth
    Meroelyth over 2 years

    I'm trying to put my notification on top of notification area.

    A solution is to set the parameter "when" to my notification object with a future time like:

    notification.when = System.currentTimeMills()*2; 
    

    The code that I'm using in this:

    long timeNotification = System.currentTimeMillis()*2;
    Notification notification = new Notification(statusIcon,c.getResources().getString(R.string.app_name),timeNotification);
    notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
    notification.when = timeNotification;
    notification.priority = Notification.PRIORITY_MAX;
    

    but some apps (like Facebook) are able to put a simple notification with their current time over mine.

    If I refresh my notification it remains under these ones.

    What parameters I have to set to put my Notification to the top of the notifications area?

    • HitOdessit
      HitOdessit over 11 years
      what Android OS version you are testing on?
    • Meroelyth
      Meroelyth over 11 years
      Android 4.1.1 Jelly Bean
  • Meroelyth
    Meroelyth over 11 years
    Thanks for your help. I edit my question to show you my code. I already use your suggestions.
  • Meroelyth
    Meroelyth over 11 years
    Thanks for your help. I edit my question to show you my code. I already use your suggestions.
  • Jagdeep Singh
    Jagdeep Singh over 11 years
    So what is the result you are getting ?
  • Meroelyth
    Meroelyth over 11 years
    Some apps are able to put their notifications over mine.
  • Shruti
    Shruti about 7 years
    I want to trigger heads-up notification but setting priority to PRIORITY_HIGH isn't working for me.
  • Harish Vishwakarma
    Harish Vishwakarma almost 7 years
    Apps can still take over the top position
  • Sagar Nayak
    Sagar Nayak over 6 years
    priority max is deprecated.
  • Ali Nadalizadeh
    Ali Nadalizadeh almost 5 years
    PRIORITY_MAX is deprecated, use NotificationManager.IMPORTANCE_HIGH instead.