问题
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:
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?
回答1:
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.
来源:https://stackoverflow.com/questions/45485460/is-it-possible-to-put-a-foreground-service-notification-in-a-notification-channe