NitZRobotKoder
NitZRobotKoder

Reputation: 1096

How to get Override Do not Disturb Settings Value?

How do I get Override Do Not Disturb Settings state in Android for my own package app?

enter image description here

Upvotes: 15

Views: 6658

Answers (1)

Adinia
Adinia

Reputation: 3731

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().

Upvotes: 14

Related Questions