18

How to show android notification on top? setPriority(Notification.PRIORITY_MAX)

as Notification.PRIORITY_MAX is deprecated what is the alternative?

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Notification Title")
                .setContentText("Notification Text")
                .setPriority(Notification.PRIORITY_MAX)
                .setAutoCancel(true);
Ankush Kapoor
  • 513
  • 1
  • 5
  • 27

5 Answers5

16

PRIORITY_MAX This constant was deprecated in API level 26. use IMPORTANCE_HIGH instead.

PRIORITY_MAX

int PRIORITY_MAX

This constant was deprecated in API level 26. use IMPORTANCE_HIGH instead.

Highest priority, for your application's most important items that require the user's prompt attention or input.

Constant Value: 2 (0x00000002)

// create ios channel
        NotificationChannel iosChannel = new NotificationChannel(IOS_CHANNEL_ID,
                IOS_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
        iosChannel.enableLights(true);
        iosChannel.enableVibration(true);
        iosChannel.setLightColor(Color.GRAY);
        iosChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        getManager().createNotificationChannel(iosChannel);

https://developer.android.com/reference/android/app/Notification.html#PRIORITY_MIN

Akash kumar
  • 981
  • 3
  • 14
  • 27
  • 1
    Please focus on the question (PRIORITY_MAX). Thank you for explaining *what* to do, but also say *how* to do it (how to work with importance). – Eugen Pechanec Oct 26 '17 at 19:54
4

In android O there was introduction of Notification channels. In particular you define Channel with constructor. In documentation you can see notion of importance and that is what replaces priority.

Jakub Szczygieł
  • 1,203
  • 1
  • 13
  • 26
3

Starting from Android O (API 26) priority at notification level has been deprecated. It was replaced by importance at the channel level and the notifications must now be put in a specific channel.

If you are running on Android O or greater you have to create a channel and define the channel importance.

NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
            CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);

If you need to maintain the back compatibility with older Android version then you have to continue to define the priority at the notification level.

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            ...
            .setPriority(Notification.PRIORITY_MAX)
            ...

Unfortunately it generates the warning about PRIORITY_MAX deprecation.

It can be avoid using NotificationCompat version of the PRIORITY_MAX constant.

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
        ...
        .setPriority(NotificationCompat.PRIORITY_MAX)
        ...
cristallo
  • 1,951
  • 2
  • 25
  • 42
  • 'PRIORITY_MAX: Int' is deprecated. Deprecated in Java – BIS Tech Aug 02 '21 at 12:07
  • @BloodLoss As I said in the answer Notification.PRIORITY_MAX is deprecated but not NotificationCompat.PRIORITY_MAX. It is useful only if you have to maintain the back compatibility with android older than Android O. – cristallo Aug 03 '21 at 09:30
1

Only Three step to follow for Android O notification

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "CHANNEL_ID")

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                CharSequence name = "Hello";// The user-visible name of the channel.
                int importance = NotificationManager.IMPORTANCE_HIGH;
                NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
                mNotificationManager.createNotificationChannel(mChannel);
            }

mNotificationManager.notify(notificationId, notificationBuilder.build());
1

Working code for all android version to set Maximum priority.

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_HIGH);

    // Configure the notification channel.
    notificationChannel.setDescription("Channel description");
    notificationChannel.enableLights(true);
    notificationChannel.setLightColor(Color.RED);
    notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
    notificationChannel.enableVibration(true);
    notificationManager.createNotificationChannel(notificationChannel);
}


NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

notificationBuilder.setAutoCancel(true)
        .setDefaults(Notification.DEFAULT_ALL)
        .setWhen(System.currentTimeMillis())
        .setSmallIcon(R.drawable.ic_launcher)
        .setTicker("Hearty365")
        .setPriority(Notification.IMPORTANCE_HIGH)
        .setContentTitle("Default notification")
        .setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
        .setContentInfo("Info");

notificationManager.notify(/*notification id*/1, notificationBuilder.build());
Aks4125
  • 4,522
  • 4
  • 32
  • 48