54

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?

Iain Smith
  • 9,230
  • 4
  • 50
  • 61
user1719863
  • 787
  • 1
  • 8
  • 13
  • if this worked for you , then ou should add it as an answer. – ProllyGeek Dec 08 '14 at 23:47
  • 1
    Please note, heads-up notifications can be EXTREMELY annoying while watching media or using any app -- not surprisingly, almost every app uses the top of the screen. They cast a shadow over the top third of the screen (tested on my 8" Tegra Shield). If you MUST use these (heaven knows why), at least make disabling them within your app an option, please! –  Feb 21 '15 at 05:13
  • 1
    @user1499731 Android has native support for disabling notification peeking for an app IIRC. – Mygod Mar 06 '16 at 00:02
  • setDefaults(Notification.DEFAULT_VIBRATE) works – Kit Oct 18 '17 at 07:20
  • I have posted a brief on showing heads-up notification at https://medium.com/@md.noor.asad/heads-up-push-notification-from-rest-and-firebase-when-app-is-in-background-or-foreground-6c4457fb6d4b – Asad Feb 26 '19 at 16:31
  • You may also follow my answer https://stackoverflow.com/a/54890459/4932661 – Asad Feb 26 '19 at 17:05

9 Answers9

87

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.

Mygod
  • 2,077
  • 1
  • 19
  • 43
  • This is not true - I recently got a crash report for a SecurityException on a Samsung Android 4 device using this method – Daiwik Daarun Dec 31 '15 at 21:33
  • @DaiwikDaarun Is it thrown by `setVibrate`? If it's only reproducible on Android 4.x, we can add a runtime version check. – Mygod Jan 01 '16 at 02:29
16

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

MLS
  • 237
  • 1
  • 5
  • 2
    `Notification.PRIORITY_HIGH` is high enough, you don't have to (and probably shouldn't) set `PRIORITY_MAX` only because you want your notifications to be shown as heads-up. – Marcel Bro Nov 27 '14 at 18:40
  • 7
    It appears that either a sound and/or vibrate has to be set on the notification for it to show as "heads up". This actually makes sense, since it would be strange to see a silent "heads up" notification, and it may not be obvious that it's a system notification otherwise. – Steven Byle Dec 12 '14 at 20:45
  • In Android M it's now possible to disable heads up notifications/peeking on a per app basis. So if this peeking disabled, whatever you do, your heads up notifications won't show. Keep this in mind . – MLS Jun 17 '15 at 13:42
  • 3
    While it is poorly documented, it is here: http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Heads-up `The notification has high priority **and** uses ringtones or vibrations` – boltup_im_coding Jan 07 '16 at 19:19
8

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.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
6

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)
unixia
  • 4,102
  • 1
  • 19
  • 23
3

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.

Zeon
  • 535
  • 8
  • 23
1

Should set high priority and use ringtones or vibrations.

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

Ref: https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Heads-up

Heads-up Notifications

With Android 5.0 (API level 21), notifications can appear in a small floating window (also called a heads-up notification) when the device is active (that is, the device is unlocked and its screen is on). These notifications appear similar to the compact form of your notification, except that the heads-up notification also shows action buttons. Users can act on, or dismiss, a heads-up notification without leaving the current app.

Examples of conditions that may trigger heads-up notifications include:

  • The user's activity is in fullscreen mode (the app uses fullScreenIntent), or
  • The notification has high priority and uses ringtones or vibrations
situee
  • 2,710
  • 2
  • 22
  • 27
1

For devices running Android 8.0 (API level 26) and higher the notification channel requires high importance

new NotificationChannel("ID", "Channel Name", NotificationManager.IMPORTANCE_HIGH);
Megaetron
  • 1,154
  • 2
  • 15
  • 29
0

Add this line in your code to display heads up notification it's only working for Lollipop version

notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
Amit Desale
  • 1,281
  • 3
  • 16
  • 43
0

You don't need to set vibrate. You only need to set sound. It's less intrusive. I don't get any sound on mine, but the notification displays on top. Make sure you use PRIORITY_HIGH and DEFAULT_SOUND.

 NotificationChannel channel = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channel = new NotificationChannel("my_channel_01",
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_HIGH);
            mNotificationManager.createNotificationChannel(channel);
        }


        Notification notification =
                new NotificationCompat.Builder(this, "notify_001")
                        .setSmallIcon(R.drawable.ic_check)
                        .setContentTitle("My notification")
                        .setContentText("Hello World!")
                        .setPriority(Notification.PRIORITY_HIGH)
                        .setDefaults(NotificationCompat.DEFAULT_SOUND)
                        .setChannelId("my_channel_01").build();


        mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(0, notification);
live-love
  • 48,840
  • 22
  • 240
  • 204