14

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?

Anax
  • 312
  • 3
  • 12
  • Using notification priority PRIORITY_MIN for foreground service notifications worked fine before Android O and also on Android O in my experience. – M66B Aug 03 '17 at 16:16
  • 2
    You mean, on Android O, if you're still not targeting API level 26? Because, priorities are deprecated since API level 26, and you need to make notification channels with importances instead, in order to order your notifications. – Anax Aug 04 '17 at 09:08
  • Priority of notifications are deprecated on API level 26, but new is notification channel 'importance', so you can still achieve the same thing. See here: https://developer.android.com/reference/android/app/NotificationChannel.html#setImportance(int) – M66B Aug 04 '17 at 09:28
  • 1
    Hey, yeah, that's kinda what I said. So, you tried making a notification channel with IMPORTANCE_MIN and you made a notification that will go into that channel and then bound it to a service to make it a foreground service, and when your app is in the background (your UI isn't on screen), the notification is in the notification shade at the bottom and you are NOT getting the notification from Android System that I have posted a screenshot of? – Anax Aug 04 '17 at 09:34
  • 1
    @M66B Please refrain from posting comments about solutions if you haven't actually verified them yourself (or, state explicitly that you haven't done so). Also, always make sure you have read the question carefully. See: https://stackoverflow.com/help/how-to-answer The question here is about targeting Android O with API 26, an issue that more and more people will come across in the upcoming months. – milosmns Aug 04 '17 at 09:51
  • @milosmns as said in my first comment "in my experience", which means I am applying the suggested solution myself with success. What makes you think I didn't? (just to improvement my comments/answers) – M66B Aug 04 '17 at 11:08
  • @M66B It's more of a general comment valid for all posts. OP was looking for an answer for API 26, that's what I was mainly referring to, as I'm looking for the same solution myself. – milosmns Aug 04 '17 at 11:11
  • @Anax if you are searching for a solution to remove the Android system notification: an app cannot do this. There is however a way to remove it using adb, see here: https://www.xda-developers.com/hide-persistent-notification-android-o/ – M66B Aug 04 '17 at 11:12
  • @milosmns okay, I understand. Please note that I thoughtfully selected to comment on the question and not to answer. – M66B Aug 04 '17 at 11:14
  • @M66B Noted, hopefully we'll get an answer to this soon.. – milosmns Aug 04 '17 at 11:16
  • @M66B Hey. That notification should not show if you have a foreground service running, correct? The fact that it only appears when the notification is in an IMPORTANCE_MIN made me think that you can't run foreground service notifications in channels with that importance. As I said, when I raise the importance of the channel to IMPORTANCE_LOW, the Android System notification does not appear. That's why I'm asking you a very specific question, and your answers are usually more generic. That's probably why milosmns interceded, too. Can you please read my last question again? – Anax Aug 04 '17 at 11:27
  • @M66B Also, as far as I understand, you get the notification from Android that your service is running in the background. Have you tried raising the importance of your channel to IMPORTANCE_LOW? Android does not post the notification about my app in that situation. Could you confirm the same? Thanks. – Anax Aug 04 '17 at 11:29
  • @Anax: with importance low the notification associated with the foreground service is shown, including its icon in the status bar (the latter doesn't occur with importance min), however the Android system notification doesn't show the foreground service anymore (it still shows other foreground services of other apps though). Summarized: Android will always show a notification for running foreground services and importance min/low alters how/where these are displayed, but it is not possible to get rid of them completely. Tested on a Nexus 5X running DP4. – M66B Aug 04 '17 at 11:48
  • 1
    @Anax be aware that you need to uninstall the app to recreate a notification channel with different attributes. – M66B Aug 04 '17 at 11:48
  • @M66B Thanks for your help. I have edited the question to reflect on your findings, too. I am very interested in whether this is the intended behavior, and if so - why. Foreground service notifications are quite bothersome when they have a persistent status bar icon, in my opinion. – Anax Aug 04 '17 at 16:54
  • "so the question that remains now is why?" -- questions of the form "why did Developer X make Decision Y?" are not well-suited for Stack Overflow. The only party that can answer definitively is Developer X, who is unlikely to see the question. "Is this maybe a bug that should be reported to the tracker?" -- I doubt it. After all, they are displaying a dedicated, system-supplied `Notification` when you have `IMPORTANCE_MIN`. It seems rather unlikely that they accidentally added it. – CommonsWare Aug 04 '17 at 16:59
  • 1
    In terms of my guess as to "why?", users have voiced their opinion, very loudly, that they **hate** stuff running behind their backs. 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. Users then railed at developers for having such services, less so for cluttering up the status bar. – CommonsWare Aug 04 '17 at 17:02
  • @CommonsWare Thank you for the answers. It's a shame that it's not documented anywhere. I guess I will just make a separate channel for the ongoing notification and then not post anything to it (or maybe even delete it (though I really don't want to have deleted channels)) when we transition away from the foreground service. Whatever the case, I guess that concludes the answer. Thanks again. – Anax Aug 04 '17 at 17:15

1 Answers1

21

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.

Josh
  • 10,618
  • 2
  • 32
  • 36
  • 1
    Thanks a lot! We had a very similar feature, also had to remove it. – Anax Jan 12 '18 at 15:49
  • 1
    i guess the answer is to use a separate channel for the higher priority information, and instruct the user on how to manually reduce the visibility of the primary notification. – j__m Jan 23 '18 at 17:28