Android : How to bypass Do Not Disturb So that it plays notification audio in background

3.6k views Asked by At

I am targeting Android 8.0 and above and want my remote notifications to play the channel configured audio sound when

a) the device is in do not disturb mode (with exceptions enabled)

and

b) the device is in the background.

Notifications work as expected when not in DND mode. When in DND mode there is a visual notification when in foreground, but no audio in background.

I set my channels up as follows:

private void InitialiseNotificationChannel(NotificationManager notificationManager, string channelID, string name, string soundFilename)
{
    // create channel
    NotificationChannel channel = new NotificationChannel(channelID, name, NotificationImportance.High);

    // create sound URI
    Android.Net.Uri uri = Android.Net.Uri.Parse("android.resource://" + Application.Context.PackageName + "/raw/" + soundFilename);

    // create audio attributes
    AudioAttributes alarmAttributes = new AudioAttributes.Builder()
       .SetContentType(AudioContentType.Sonification)
       .SetUsage(AudioUsageKind.Notification).Build();

    // setup channel
    channel.SetSound(uri, alarmAttributes);
    channel.EnableVibration(true);

    channel.EnableLights(true);
    channel.LightColor = Resource.Color.red;

    long[] vibrationPattern = { 100, 200, 300, 400, 500, 600, 1000 };
    channel.SetVibrationPattern(vibrationPattern);

    // Bypass Do Not Disturb
    channel.SetBypassDnd(true);
    channel.LockscreenVisibility = NotificationVisibility.Public;

    // add channel
    notificationManager.CreateNotificationChannel(channel);
}

I also set filtering up to handle exceptions:

            if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
                 notificationManager.NotificationPolicy = new NotificationManager.Policy(NotificationPriorityCategory.Events, NotificationPrioritySenders.Any, NotificationPrioritySenders.Any);

            notificationManager.SetInterruptionFilter(InterruptionFilter.All);

I have all types of exception selected on the Device for the App, namely :

All Calls,

All Messages,

Alerts/Tasks ON,

Reminders ON

But still no audio sound when a notification is received in background.

Any ideas what I am missing ?

Thanks

1

There are 1 answers

0
M A On

Do keep that in mind that, according to the docs, the SetBypassDnd can only be modified by either the system itself (the user clicking through device settings), and the notification ranker.