How do I get Override Do Not Disturb Settings state in Android for my own package app?
Steps to enable Override Do Not Disturb modeMake sure Push Notifications and Alarm Events are turned on. Towards the top of the notifications setting screen, select Override Do Not Disturb mode and switch to Enabled.
@MobileLearner Overriding means being allowed to send notifications even though DND is enabled.
On Do Not Disturb screen, scroll down and select Calls, messages and conversations option, located under “Exceptions” section. On the next screen, tap on Calls and select Favorite contacts only option in the menu that appears.
From the Home screen, tap > Settings > Sound & notification. Tap Do not disturb. Tap Do not disturb mode, and then select Priority. Select a specific length of time for Do not disturb mode to last, or select Until turned off to end the mode when your phone is turned off, and then tap OK.
The documentation is not very clear how to show this option, just that it's possible:
On Android 8.0 (API level 26) and above, users can additionally allow notifications through for app-specific categories (also known as channels) by overriding Do Not Disturb on a channel-by-channel basis. [...] On devices running Android 7.1 (API level 25) and below, users can allow notifications through on an app by app basis, rather than on a channel by channel basis.
But according to my testes, on Android 8.0+ you have this option only for notification channels that have set the importance to Urgent, that corresponds to NotificationManager.IMPORTANCE_HIGH
. For more info about creating a channel, see Create a channel and set the importance.
On Android 5.0 to 7.1, it's said you have to use setPriority()
On Android 8.0 (API level 26) and above, importance of a notification is determined by the importance of the channel the notification was posted to. Users can change the importance of a notification channel in the system settings (figure 12). On Android 7.1 (API level 25) and below, importance of each notification is determined by the notification's priority.
So I tried with NotificationCompat.PRIORITY_MAX
, but I didn't manage to see the Override Do Not Disturb option until I also added a system-wide category,
something like:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle("Notification title")
.setContentText("Text content")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setCategory(NotificationCompat.CATEGORY_ALARM);
Now, for Android 8.0+, to see what settings an user has applied to your channel, Read notification channel settings suggests using canBypassDnd()
from getNotificationChannel()
:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = manager.getNotificationChannel(CHANNEL_ID);
channel.canBypassDnd();
}
Unfortunately, under 7.1 there doesn't seem to be any public method to get that info; the only one available for NotificationManagerCompat
is areNotificationsEnabled()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With