2

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.

user3927415
  • 415
  • 4
  • 18

1 Answers1

4

Need to do

notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY)

instead of

notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_NONE)
Rakib Ansary
  • 4,078
  • 2
  • 18
  • 29
user3927415
  • 415
  • 4
  • 18