Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to put a foreground service notification in a notification channel with IMPORTANCE_MIN?

I am currently working on transitioning an application to Android O, and I am currently working on notification channels.

I have made different channels with different importance levels and since the application has a foreground service that has to run at all times until we transition to a new architecture (more push oriented), I thought about putting that notification in a channel that has its importance set as IMPORTANCE_MIN, so that it is there, but it doesn't bother the user, and doesn't place an icon in the status bar.

However, when I do that, and I put my application in the background (with Home or Back buttons), I get an Android System notification telling me that my app is running in the background, like so:

Android System notification for a service running in the background

If I change my channel and make it use IMPORTANCE_LOW, the problem goes away, however, the notification is more prominent.

So, my question is - is it possible to do what I am trying at all? I get that the system would not allow the developers to do this, because if you have a foreground service, it should be visible to the user, but that's just a guess, and I found no documentation regarding this, and that's why I'm posting this question.

My second question is - prior to O, if you set the priority of your notification to PRIORITY_MIN, can you bind that notification to a service to make it a foreground service, or was that a no-go since always?

Edit: Confirmed that the Android System shows the notification for channels with importance IMPORTANCE_MIN (thanks, M66B), so the question that remains now is why? Does anyone know the reasoning behind this, or can find any documentation anywhere? Is this maybe a bug that should be reported to the tracker?

like image 848
Anax Avatar asked Aug 03 '17 13:08

Anax


People also ask

How do I start a foreground service with Android notification channels?

Inside the service, usually in onStartCommand() , you can request that your service run in the foreground. To do so, call startForeground() . This method takes two parameters: a positive integer that uniquely identifies the notification in the status bar and the Notification object itself.

How do I update the notification text for a foreground service in Android?

When you want to update a Notification set by startForeground(), simply build a new notication and then use NotificationManager to notify it. The key point is to use the same notification id.

What is the default importance level for foreground service notifications?

In Android O, a channel’s default importance level for foreground service notifications must be at least IMPORTANCE_LOW so that it shows an icon in the status bar. Channels using the less-prominent IMPORTANCE_MIN level will trigger an extra notification from Android at IMPORTANCE_LOW, stating that the app is using battery.

Why doesn't startforeground have a notification?

For example, in the Android 4.x timeframe, there was a bug in Android where startForeground () with a flawed Notification (e.g., no icon) would result in foreground importance but no Notification. Android 4.4 (?) changed the behavior to "repair" such a broken Notification, so users would know about the foreground service.

How to hide the startforeground service notification in Android?

Since Android 8, it is not possible to automatically hide a foreground service notification. To hide the notification, we can guide the users to open the notification channel configuration interface to hide it manually. Is StartForeground () consumes more battery? ...

Is it possible to mark a foreground service as minimum importance?

This should not be used with Service.startForeground since a foreground service is supposed to be something the user cares about so it does not make semantic sense to mark its notification as minimum importance. If you do this as of Android version O, the system will show a higher-priority notification about your app running in the background.


1 Answers

This behavior is now documented: https://developer.android.com/reference/android/app/NotificationManager.html#IMPORTANCE_MIN

Min notification importance: only shows in the shade, below the fold. This should not be used with Service.startForeground since a foreground service is supposed to be something the user cares about so it does not make semantic sense to mark its notification as minimum importance. If you do this as of Android version O, the system will show a higher-priority notification about your app running in the background.

And also here: https://material.io/guidelines/patterns/notifications.html#notifications-settings

In Android O, a channel’s default importance level for foreground service notifications must be at least IMPORTANCE_LOW so that it shows an icon in the status bar.

Channels using the less-prominent IMPORTANCE_MIN level will trigger an extra notification from Android at IMPORTANCE_LOW, stating that the app is using battery.

Sidenote: This is a real pain for us, since prior to O we used to dynamically switch between PRIORITY_DEFAULT and PRIORITY_MIN when our foreground notification had no interesting information to present. With channels we can't change the IMPORTANCE dynamically anymore, and had to remove that feature.

like image 194
Josh Avatar answered Nov 03 '22 01:11

Josh