14

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

enter image description here

Adinia
  • 3,722
  • 5
  • 40
  • 58
NitZRobotKoder
  • 1,046
  • 8
  • 44
  • 74

1 Answers1

13

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

Adinia
  • 3,722
  • 5
  • 40
  • 58
  • I have custom notification which will not use standard notification instead use mediaplayer or soundpool to play tones. Can i get hold of the settings value somehow? Or this settings is applicable only if standard notification are used? – NitZRobotKoder Jun 12 '18 at 09:53
  • I think it depends on the way these custom notifications are set; if you can get the CHANNEL_ID, then it's possible to get the state, as I have added in my answer. – Adinia Jun 12 '18 at 10:02
  • thanks..As i said have not used android standard notification so no concept of CHANNEL_ID. How to get hold in android < O? – NitZRobotKoder Jun 12 '18 at 10:08
  • 1
    It doesn't seem to be a way to get it for <7.1; the only public method available is [`areNotificationsEnabled()`](https://developer.android.com/reference/android/support/v4/app/NotificationManagerCompat.html#areNotificationsEnabled()) – Adinia Jun 12 '18 at 10:17