Turn on do not disturb programmatically, with exceptions

1.5k Views Asked by At

I have an android app, and I need to turn on the Do Not Disturb button programmatically, but with exceptions, that I can play media. I did succeed with turning on the Do Not Disturb with this code :

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (notificationManager.isNotificationPolicyAccessGranted) {
        Log.d(Globals.LOG_TAG, "has permissions")
    } else {
        Log.d(Globals.LOG_TAG, "does not have permissions")
        val intent = Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS)
        startActivity(intent)
    }
} else {
    Log.d(Globals.LOG_TAG, "device does not support do not disturb feature")
}
notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE)

and added the permissions :

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

but couldn't play media on the app because of the Do Not Disturb. But I saw that I can set exceptions (for calls, alarms, media...), and I want to add an exception to media programmatically.

How can I achieve that? Thank you.

1

There are 1 best solutions below

0
On BEST ANSWER

Need to do

notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY)

instead of

notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE)