android show notification with a popup on top of any application

Android

Android Problem Overview


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());
}

Android Solutions


Solution 1 - Android

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 :-)

Solution 2 - Android

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

Solution 3 - Android

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.

Solution 4 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjust_userView Question on Stackoverflow
Solution 1 - AndroidRediOne1View Answer on Stackoverflow
Solution 2 - AndroidAnders8View Answer on Stackoverflow
Solution 3 - Androidjuan IsazaView Answer on Stackoverflow
Solution 4 - AndroidmboView Answer on Stackoverflow