71

With the below code my notification are only added to the notification bar, no popup style message is displayed like if you would receive a whatsapp message when you're in another application. What makes that happen to a notification?

private void sendNotification(int distance, ViewObject viewObject) {
    Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notificationIntent.putExtra("path", viewObject.getPath());
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(notificationIntent);
    PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(Integer.parseInt(viewObject.getRefId()), PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
    bigText.bigText(String.format(getString(R.string.notification), viewObject.getTitle()));
    bigText.setBigContentTitle(getString(R.string.hello));

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.ic_wald_poi)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_poi))
            .setColor(getResources().getColor(R.color.primary))
            .setContentTitle(getString(R.string.hello))
            .setContentIntent(notificationPendingIntent)
            .setContentText(String.format(getString(R.string.notification), viewObject.getTitle()))
            .setDefaults(Notification.DEFAULT_ALL)
            .setStyle(bigText);

    builder.setAutoCancel(true);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, builder.build());
}
just_user
  • 11,769
  • 19
  • 90
  • 135

4 Answers4

144

If you want use Heads-up Notifications like this:

enter image description here

You must change Notification priority or NotificationChannel importance.

The notification priority, set by setPriority(). The priority determines how intrusive the notification should be on Android 7.1 and lower. (For Android 8.0 and higher, you must instead set the channel importance)

On Android 7.1 (API level 25) and lower:

  • Set notification priority to NotificationCompat.PRIORITY_HIGH or NotificationCompat.PRIORITY_MAX.
  • Set ringtone and vibrations - you can use setDefaults(Notification.DEFAULT_ALL)

Android 8.0 (API level 26) and higher:

  • Set notification channel priority to NotificationManager.IMPORTANCE_HIGH

Notification:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
            builder.setSmallIcon(R.drawable.ic_wald_poi)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_poi))
                    .setColor(getResources().getColor(R.color.primary))
                    .setContentTitle(getString(R.string.hello))
                    .setContentIntent(notificationPendingIntent)
                    .setContentText(String.format(getString(R.string.notification), viewObject.getTitle()))
                    .setDefaults(NotificationCompat.DEFAULT_ALL)
                    .setStyle(bigText)
                    .setPriority(NotificationCompat.PRIORITY_HIGH) // or NotificationCompat.PRIORITY_MAX

Notification channel:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    // Create the NotificationChannel
    val name = getString(R.string.notification_channel_name)
    val descriptionText = getString(R.string.notification_channel_description)
    val importance = NotificationManager.IMPORTANCE_HIGH
    val mChannel = NotificationChannel(NOTIFICATION_CHANNEL_ID, name, importance)
    mChannel.description = descriptionText
    // Register the channel with the system; you can't change the importance
    // or other notification behaviors after this
    val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.createNotificationChannel(mChannel)
}

Important

If you'd like to further customize your channel's default notification behaviors, you can call methods such as enableLights(), setLightColor(), and setVibrationPattern() on the NotificationChannel. But remember that once you create the channel, you cannot change these settings and the user has final control of whether these behaviors are active. Other option is to uninstall and install application again. Read more


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

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

Priority:

Notification.PRIORITY_HIGH and Notification.PRIORITY_MAX was deprecated in API level 26. use NotificationCompat instead.

Here is more info :-)

RediOne1
  • 10,389
  • 6
  • 25
  • 46
  • if i want to my activity touch events as it is and not dismiss this notification than it is possible? – Vishal Patoliya ツ May 11 '17 at 10:41
  • try `setAutoCancel(false);` – RediOne1 May 11 '17 at 11:12
  • 2
    Now `Notification.PRIORITY_HIGH` and `Notification.PRIORITY_MAX` are Deprecated. – pRaNaY Nov 01 '17 at 08:45
  • 4
    The priority is not sufficient. There has to be a ringtone or vibration as described in my answer. The notification in your example may have ringtone and vibration added with `setDefaults()` but you did not mention it. – mbo Nov 26 '17 at 09:54
  • 44
    For those who follow this instructions and try them. I have observed, and it did cost me hours, that when changing the importance in code it is rather helpful to uninstall the app and install it from scratch. Otherwise you may not see any change in terms of importance and appearance of the notification widget. – Hermann Klecker May 28 '18 at 16:04
  • 2
    @Herman you are the god man, this is accepted answers but not working. I red your comment and tried its working now. – Surya Reddy Mar 05 '19 at 13:26
  • @SuryaReddy This is related to `Notification Channel`. When you once create notification channel you can't edit it. Only user can make further changes in appliaction settings. There is remark about this situation: `If you'd like to further customize your channel's default notification behaviors...` – RediOne1 Mar 06 '19 at 08:43
  • What is so confusing is that you have to use the NotificationMANAGER static int not the NotificationCompat static for +Android 8, both will compile, only one will work... – CaptainCrunch Jul 25 '19 at 22:10
  • @RediOne1 The above suggestions works fine when the app is in foreground, but for some reason **popup is not showing when the app is in background**. I have set both `NotificationCompat.PRIORITY_HIGH` and `NotificationCompat.DEFAULT_ALL`. – Ankush Chauhan Nov 17 '19 at 17:24
  • @AnkushChauhan use `NotificationManager.IMPORTANCE_HIGH` for `NotificationChannel` – RediOne1 Nov 25 '19 at 10:46
  • 1
    @RediOne1 Yes, I have used this as well. That's why it seems strange to me what could be the reason for this behaviour. Also I want to clarify that when the app is in background only the popup is missing, the notification icon comes with the sound. – Ankush Chauhan Nov 25 '19 at 14:59
  • @AnkushChauhan Maybe it is problem with that specific phone, and it will work on other phones? Or Google changed implementation, and now it isn't working in background. – RediOne1 Nov 26 '19 at 09:55
  • As an FYI, this can be configured by the user as well to not show the heads up notification (at least on Samsung Note 10) and when Do Not Disturb is enabled. Settings -> Notifications -> Do not disturb -> Hide Notifications -> Don't pop up notifications can be toggled to either show or not show the heads up notification. – tim.paetz May 19 '20 at 17:00
36

You have to set the notification priority to Notification.PRIORITY_HIGH or Notification.PRIORITY_MAX. I had to also do .setDefaults(Notification.DEFAULT_ALL).

Ben H
  • 3,855
  • 1
  • 26
  • 33
MotoRidingMelon
  • 2,347
  • 2
  • 21
  • 28
9

Below API level 26: Set Notification.PRIORITY_HIGH or Notification.PRIORITY_MAX in the Builder when sending the message.

API level 26 or above:

Set channel priority high

Important:

Once the channel has a established priority it cannot be changed. If it was in LOW will not show the popup, unless you reinstall the APP.

juan Isaza
  • 3,646
  • 3
  • 31
  • 37
  • 5
    Thanks, I can confirm that the notification finally pop up after reinstall. Make sure the importance of channel never change then – Irfandi D. Vendy Jun 23 '20 at 10:56
  • 2
    Thank you, this part "Once the channel has a established priority it cannot be changed. If it was in LOW will not show the popup, unless you reinstall the APP" was the key of the problem. After I set up the new priorities in the code, I fixed just changing my chanel id to a new one. – Tommy89 May 07 '21 at 08:24
5

The Android system is deciding wheter a notification is displayed as Heads-up notification. There are several conditions which may trigger a Heads-up notification:

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

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

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

So if you're running Android 7.1 and lower, be sure to add a ringtone or vibration as well as the high priority. For Android 8.0 and higher, change the priority to high importance.

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
    // ...
    .setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE)
    .setPriority(NotificationCompat.PRIORITY_HIGH); // or HIGH_IMPORTANCE for Android 8.0
mbo
  • 4,611
  • 2
  • 34
  • 54