Niklas Dada
Niklas Dada

Reputation: 401

Android: set "do not disturb" (setInterruptionFilter) for all notifications besides the apps audio stream

I have built an app that plays audio which must not be interrupted during it plays, because it provides timecode-information to other devices, and will not work properly when the device plays something different in between (e.g. a phone call).

So I need a solution to mute notifications while playing audio. I found out so far:

Probably a solution is, to set the DND-mode manually and start the app and the stream afterwards. But how is it possible to send the user directly to the DND-menu, so that he/she could enable DND-mode and return to the app by pressing the back button? I only found the Settings-Actions:

    ACTION_NOTIFICATION_LISTENER_SETTINGS
    ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS
    ACTION_SOUND_SETTINGS

which are not the right screens for that. Maybe it is provided somehow to open the quick settings of the DND mode?

So to shorten it up, what I need to do:

User starts audio -> user is asked if he wants to active dnd for no interuption of the audio-playback -> audio-playback is started -> user stops audio -> DND is deactivated

Thanks in advance for ideas on that!

Upvotes: 2

Views: 3697

Answers (3)

Wowo Ot
Wowo Ot

Reputation: 1529

And you also need to add this permission

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

and call this on your Activity

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            NotificationManager n = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
            if(n.isNotificationPolicyAccessGranted()) {
            } else {
                startActivityForResult(new Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS), 0);
            }
        }

Upvotes: 0

Khay
Khay

Reputation: 993

In Kotlin this can be done as..

 notificationManager.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_PRIORITY)
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    val policy =
                        NotificationManager.Policy(Policy.PRIORITY_CATEGORY_MEDIA, 0, 0)
                    notificationManager.notificationPolicy = policy
                }

NOTE: This is only possible in Android API level 28 and above since the policy PRIORITY_CATEGORY_MEDIA was added in that API.

There are other Policy categories that might be helpful in other cases. like PRIORITY_CATEGORY_REMINDERS, PRIORITY_CATEGORY_EVENTS, PRIORITY_CATEGORY_MESSAGES, PRIORITY_CATEGORY_CALLS, PRIORITY_CATEGORY_REPEAT_CALLERS etc.. read more about them here NotificationManager.Policy Official doc

Upvotes: 0

oldium
oldium

Reputation: 181

You are basically disabling all sounds with INTERRUPTION_FILTER_NONE. Use INTERRUPTION_FILTER_PRIORITY and configure notification policy to allow media playback like the following:

NotificationManager.setInterruptionFilter(
   NotificationManager.INTERRUPTION_FILTER_PRIORITY );
NotificationManager.setNotificationPolicy(
   new NotificationManager.Policy( NotificationManager.Policy.PRIORITY_CATEGORY_MEDIA,
      0, 0 ) );

You can also suppress some effects like lights by adding additional parameter to setNotificationPolicy – see Android help for details.

Upvotes: 2

Related Questions