3

I have a notification that contains an action like this:

enter image description here

The action is always shown, I'd like to hide it by default so the user has to press the button at the top right to show the action so that the notification occupies less space.

I've read about using a low notification priority by calling setPriority on the NotificationCompat.Builder before posting the ongoing notification from my foreground service, but this doesn't seem to work. I'm using Android SDK version 28.

0ne_Up
  • 471
  • 3
  • 9
  • 24
  • please share how do you set the priority – Zain Nov 13 '20 at 06:34
  • check this post https://stackoverflow.com/a/55757603/10778405 – S T Nov 13 '20 at 06:38
  • @Zain I've edited my question, by calling `setPriority` on the `NotificationCompat.Builder` before posting the ongoing notification from my foreground service – 0ne_Up Nov 13 '20 at 06:39

1 Answers1

2

Starting from API 26, using PRIORITY_LOW is deprecated, instead you can use IMPORTANCE_LOW when you create the channel

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

Also keep the low priority in the NotificationCompat.Builder with builder.setPriority(NotificationCompat.PRIORITY_LOW)

Zain
  • 37,492
  • 7
  • 60
  • 84
  • Thanks, but that also doesn't seem to work. And yes I reinstalled the application so that the notification channel is recreated. I also tried IMPORTANCE_MIN which even hides the icon from the status bar (which is not what I want) but the action is still shown... :( – 0ne_Up Nov 13 '20 at 07:34
  • 2
    Please try this on multiple devices and different vendors.. Some device models like Xiaomi has their own settings for showing notifications – Zain Nov 13 '20 at 10:47
  • It behaves the same way in the emulator, and I tried a few different Android versions. The action does seem to get hidden when there are other notifications, whether or not I use a low importance on my notification. But I want to always hide it... – 0ne_Up Nov 14 '20 at 12:26
  • If your notification is on top of all other notifications, the action will always be shown by default. I just tested this out and came across that you still have to use `builder.setPriority(NotificationCompat.PRIORITY_LOW)` along with keeping `NotificationManager.IMPORTANCE_LOW` in your channel .. and now the action is gone for me .. wish this helps you – Zain Nov 14 '20 at 16:15