Android How to Turn on Do Not Disturb (Dnd) Programmatically

Android how to turn on do not disturb (dnd) programmatically

I found this solution:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE);

It requires:

<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />

Which as far as I can tell does not pop up a request dialog when doing requestPermissions(). It has to be granted through the settings menu Settings -> Sound & Notifcation -> Do Not Disturb Access.

This is on SDK 23 Marshmallow.

Turn on do not disturb programmatically, with exceptions

Need to do

notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY)

instead of

notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE)

Android: Detect Do Not Disturb status?

I'm bringing the necro with this post, but was just searching for a solution to the same problem and came past this post as I did, so am leaving a solution for future ref.

I couldn't find an "official" way to do this, but did find a value that can return the DnD state as an integer. The feature is called "zen_mode" internally, so you can check the current value with the following code:

Global.getInt(getContentResolver(), "zen_mode")

That'll return:

  • 0 - If DnD is off.
  • 1 - If DnD is on - Priority Only
  • 2 - If DnD is on - Total Silence
  • 3 - If DnD is on - Alarms Only

When the setting is priority only, it returns nothing, but you can figure out that state by assuming no news = Priority Messages mode. This must have been a bug, as now it's a year on, this now returns a value just like the other states. No idea when it was fixed, but it now works as you'd expect.

I tried with a settings observer too, which works but only for certain state transitions, so I think polling periodically is the best bet, until an "official" way to listen for this state change becomes available.

I've included both observer and polling methods of getting the value in this Gist. Hopefully it'll help/save someone else some time.

Update 15th March 2017: Thanks to David Riha for the comment. Added 1 - Priority Only state to answer, revised the Gist to recognise that state, and to output unknown values to log in case any other devices or future updates decide to return a different value.

Program do not disturb mode

Use that method:

private void setRingerMode(Context context, int mode) {

NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Check for DND permissions for API 24+
if (android.os.Build.VERSION.SDK_INT < 24 || (android.os.Build.VERSION.SDK_INT >= 24 && !nm.isNotificationPolicyAccessGranted())) {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(mode);
 }
}

Where mode parameter can be AudioManager.RINGER_MODE_SILENT or
AudioManager.RINGER_MODE_NORMAL

How to get Override Do not Disturb Settings Value?

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



Related Topics



Leave a reply



Submit