Forum Xamarin Xamarin.Android

How to set “Do Not Disturb Custom Exception”

I would like to set the following settings related to Do Not Disturb :

notifications/<app>/fcm notifications/'Do Not Disturb Custom Exception'

and

sounds/do not disturb/'Allow Custom Exceptions'

during initialisation, so that we continue to receive notifications in the event that the user sets "Do Not Disturb" for the device

Can anyone suggest how I can do this?

Thanks.

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited November 2019

    we continue to receive notifications in the event that the user sets "Do Not Disturb" for the device

    Set notification's Category settings to Notification.CategoryCall or Notification.CategoryAlarm. When the user configures Do not disturb to block all interrupts except for phone calls (as illustrated in the above screenshot), Android allows notifications with a category setting of Notification.CategoryCall to be presented while the device is in Do not disturb mode. Note that Notification.CategoryAlarm notifications are never blocked in Do not disturb mode.

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
    ...
    builder.SetCategory (Notification.CategoryCall); // or Notification.CategoryAlarm
    

    Tutorial: https://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/notifications/local-notifications#category-settings

  • MarthrMarthr Member ✭✭

    Thank you for this. I assume this is setting the category for a single notification. When we send a notification we use a FirebaseAdmin.Messaging.Message.Notification from our sever. There does not appear to be a Category property on that object. We cannot manipulate the notification on the client side, because we are looking at the scenario where the client is in background and so no code will be executing.

    Applying CatergoryAlarm on each notification would be a neat work around, but I cant see how to do that from the server.

    The way the question was originally couched was for the app to change its own setting for all notifications, so that the user did not have to do this.

    But CategoryAlarm might be a better way, but it is not clear how to do this from the server side ?

  • JarvanJarvan Member, Xamarin Team Xamurai

    Try to detect the OnMessageReceived event and anlayze the message to send a local notification. Set Category for the local notification.

    [Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MyFirebaseMessagingService : FirebaseMessagingService
    {
        const string TAG = "MyFirebaseMsgService";
        public override void OnMessageReceived(RemoteMessage message)
        {
           //
        }
    }
    

    Tutorial: https://docs.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=windows#foreground-notifications

  • MarthrMarthr Member ✭✭

    Thank you for your reply. Unfortunately I am sending alert notifications, not data ones, so that event does not fire in the background.

Sign In or Register to comment.