Cannot Set Notification Priority to Low on Android Oreo - android

It seems that lowest notification level you can set when creating a Notification Channel on Android Oreo is Medium.
This is the case even when using NotificationManager.IMPORTANCE_NONE.
NotificationChannel sendingChannel = new NotificationChannel("SENDING", "Sending", NotificationManager.IMPORTANCE_NONE);
NotificationManager.IMPORTANCE_NONE, NotificationManager.IMPORTANCE_MIN, and NotificationManager.IMPORTANCE_LOW all set the Notification Channel importance to Medium.
There does not appear to be a way to set the notification importance to low on Android Oreo.

I ran into this issue when trying to set a notification channel to Low for use with a foreground service. I can confirm in that case, the level gets increased to Medium by the system.
See this stack overflow question/answer for details: Default priority of notification channel on Android 8
Documentation for the behavior exists (again, assuming you're using a foreground service) although I would argue it's a bit unclear: https://developer.android.com/reference/android/app/NotificationManager#IMPORTANCE_MIN for details

Related

startForegroundService - Specify a custom 'background app running..' notification?

I have a foreground service that I launch via startForegroundService.
All works great.
The only thing I am unable to figure out is how to / if its possible to customize the "...is running in the background' notification.
The notification I am sending over to startForeground looks like this:
Notification notification = notificationBuilder.setOngoing(true)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setSmallIcon(R.drawable.ic_notif_icon)
.setContentTitle("My title")
.setContentText("My content")
.build();
startForeground(1, notification);
I am then presented by android with the standard "...running in background" notification, with my custom one no where to be seen.
Am I doing something wrong?
Not looking to hide it or anything nefarious, but would like to use it more as a status of what the background service is actually doing... Which sounds like an ideal use case for this sort of thing.
For what its worth, I am running Android 8 and targeting SDK 26 as that is the latest available for my handset.
Thanks!
As per the Create and Manage Notification Channels guide:
Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel.
Your notification is not appearing because you do not set a notification channel as per the note on that same page:
Caution: If you target Android 8.0 (API level 26) and post a notification without specifying a notification channel, the notification does not appear and the system logs an error.
Note: You can turn on a new setting in Android 8.0 (API level 26) to display an on-screen warning that appears as a toast when an app targeting Android 8.0 (API level 26) attempts to post without a notification channel. To turn on the setting for a development device running Android 8.0 (API level 26), navigate to Settings > Developer options and enable Show notification channel warnings.

Will the notification channel importance be overriden by individual notification importance?

The question is for Android O and above.
For example, I created a notification channel called "default" and set it IMPORTANCE_DEFAULT. Then I've created a notification, added it to the channel and put IMPORTANCE_HIGH in its builder. What will be the final priority of the notification?
I didn't find the answer in docs, they say that individual ones are required to support older versions:
To support devices running Android 7.1 (API level 25) or lower, you
must also call setPriority() for each notification, using a priority
constant from the NotificationCompat class.
https://developer.android.com/training/notify-user/channels#importance
In my experience, on Android O and above, NotificationChannel's importance seems to take precedence over individual Notifications' priority levels.
In my own app I manually specify each Notification's priority levels as well to conform to Google's advice that you quoted. I just tried setting a Notification's priority to PRIORITY_MAX and send it out via a NotificationChannel with importance IMPORTANCE_DEFAULT and the notification appeared in the status bar without sliding down (like a heads-up display) like the case when you use a NotificationChannel with IMPORTANCE_HIGH.

Without using the delete, is possible to update a Notification Channel Importance level after it was created?

Without deleting the channel.
After created a notification channel on Android, is that possible to update the importance level of a specific channel though application update?
Just calling the createNotificationChannel again or reinstalling the app is not being enough.
No, it's not possible.
As explained in the documentation:
You can't programmatically modify the behavior of a notification
channel after it's created and submitted to the notification manager;
the user is in charge of those settings after creation.
According to the specs, you can invoke createNotificationChannel again, with a lower importance level only:
The importance of an existing channel will only be changed if the new
importance is lower than the current value and the user has not
altered any settings on this channel.

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:
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?
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.

Heads-up Notification - Android Lollipop

I'm trying to show a notification-type heads-up but I could not. What I tried
final Notification.Builder notif = new Builder(getApplicationContext())
.setContentTitle(getString(R.string.title))
.setContentText(getString(R.string.text))
// .setTicker(getString(R.string.tick)) removed, seems to not show at all
// .setWhen(System.currentTimeMillis()) removed, match default
// .setContentIntent(contentIntent) removed, I don't neet it
.setColor(Color.parseColor(getString(R.color.yellow))) //ok
.setSmallIcon(R.drawable.ic_small) //ok
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
// .setCategory(Notification.CATEGORY_CALL) does not seem to make a difference
.setPriority(Notification.PRIORITY_MAX); //does not seem to make a difference
// .setVisibility(Notification.VISIBILITY_PRIVATE); //does not seem to make a difference
mNotificationManager.notify(Constants.NOTIFICATION_ID, notif.build());
The notification is shown only as an icon in the bar.
I'm using API 21 on API 21 emulator (not L preview)
I have tried:
android:Theme.Holo.NoActionBar,
android:Theme.Holo.NoActionBar.Fullscreen
and NotificationCompat.Builder
SDK examples are not available. does anyone know how to do it?
I made it working by adding:
.setDefaults(Notification.DEFAULT_VIBRATE)
is this the best way?
According to Notifications, you are required to set a vibrate or ringtone to make Heads-up work. However, here's a quick hack that doesn't require VIBRATE permission to produce a head-up notification:
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= 21) notificationBuilder.setVibrate(new long[0]);
EDIT:
Don't abuse heads-up notification. See here for when to use heads-up notification:
MAX: For critical and urgent notifications that alert the user to a condition that is time-critical or needs to be resolved before they can continue with a particular task.
HIGH: Primarily for important communication, such as messages or chat events with content that is particularly interesting for the user. High-priority notifications trigger the heads-up notification display.
According to Google:
https://developer.android.com/design/patterns/notifications.html
If a notification's priority is flagged as High, Max, or full-screen, it gets a heads-up notification.
So the following code should generate an heads-up notification:
.setPriority(Notification.PRIORITY_MAX)
Should be enough. But apparently the .setDefaults(Notification.DEFAULT_VIBRATE) has to be set also. Hopefully Google will fix this in their final release of Android 5.0.
Not sure if bug or feature...
All my apps doesn´t show the Notification, for example i have a Nexus 6 with Android 5.1.1, but i think this is an issuse since Android 5.0, i had to set:
.setPriority(Notification.PRIORITY_HIGH)
Correctly set and manage notification priority
Android supports a priority flag for notifications. This flag allows you to influence where your notification appears, relative to other notifications, and helps ensure that users always see their most important notifications first. You can choose from the following priority levels when posting a notification:
MAX Use for critical and urgent notifications that alert the user to a condition that is time-critical or needs to be resolved before
they can continue with a particular task.
HIGH Use primarily for important communication, such as message or chat events with content that is particularly interesting for the
user. High-priority notifications trigger the heads-up notification
display.
DEFAULT Use for all notifications that don't fall into any of the other priorities described here and if the application does not
prioritize its own notifications
LOW Use for notifications that you want the user to be informed about, but that are less urgent. Low-priority notifications tend to
show up at the bottom of the list, which makes them a good choice for
things like public or undirected social updates: The user has asked to
be notified about them, but these notifications should never take
precedence over urgent or direct communication.
MIN Use for contextual or background information such as weather information or contextual location information. Minimum-priority
notifications do not appear in the status bar. The user discovers them
on expanding the notification shade.
To set the priority, use the setPriority function (introduced in API 16) alongwith setDefaults (added in API 11) of Notification Builder. Choose the priority DEFAULT, HIGH, LOW, MAX, MIN as per the requirement of your app. Defaults can also be chosen here.
A small snippet:
notification = NotificationBuilder(service)
notification.setPriority(Notification.PRIORITY_MAX)
notification.setDefaults(Notification.DEFAULT_ALL)
Please check that your phone is not in “silent” or “do not disturb” mode. I spent day before I found it. I just leave this comment for those who get the same problem and found this question.
Should set high priority and use ringtones or vibrations.
notificationBuilder.setDefaults(Notification.DEFAULT_ALL);
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
Ref: https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Heads-up
Heads-up Notifications
With Android 5.0 (API level 21), notifications can appear in a small
floating window (also called a heads-up notification) when the device
is active (that is, the device is unlocked and its screen is on).
These notifications appear similar to the compact form of your
notification, except that the heads-up notification also shows action
buttons. Users can act on, or dismiss, a heads-up notification without
leaving the current app.
Examples of conditions that may trigger heads-up notifications
include:
The user's activity is in fullscreen mode (the app uses fullScreenIntent), or
The notification has high priority and uses ringtones or vibrations
For devices running Android 8.0 (API level 26) and higher the notification channel requires high importance
new NotificationChannel("ID", "Channel Name", NotificationManager.IMPORTANCE_HIGH);
Add this line in your code to display heads up notification it's only working for Lollipop version
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
You don't need to set vibrate. You only need to set sound. It's less intrusive. I don't get any sound on mine, but the notification displays on top. Make sure you use PRIORITY_HIGH and DEFAULT_SOUND.
NotificationChannel channel = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channel = new NotificationChannel("my_channel_01",
"Channel human readable title",
NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(channel);
}
Notification notification =
new NotificationCompat.Builder(this, "notify_001")
.setSmallIcon(R.drawable.ic_check)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(Notification.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.setChannelId("my_channel_01").build();
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, notification);

Categories

Resources