Share via


Critical alerts in Xamarin.iOS

With iOS 12, apps can send critical alerts. Critical alerts play a sound regardless of whether or not Do Not Disturb is enabled or the ringer switch is off. These notifications are disruptive and should only be used when users must take immediate action.

Custom critical alert entitlement

To display critical alerts in your app, first request a custom critical alert notifications entitlement from Apple.

After receiving this entitlement from Apple and following any associated instructions about how to configure your app to use it, add the custom entitlement to your app's Entitlements.plist file(s). Then, configure your iOS Bundle Signing options to use Entitlements.plist when signing the app on both simulator and device.

Request authorization

An app's notification authorization request prompts the user to allow or disallow an app's notifications. If the notification authorization request asks for permission to send critical alerts, the app will also give the user a chance to opt in to critical alerts.

The following code requests permission to send both critical alerts and standard notifications and sounds by passing the appropriate UNAuthorizationOptions values to RequestAuthorization:

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
    UNUserNotificationCenter center = UNUserNotificationCenter.Current;
    var options = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Sound | UNAuthorizationOptions.CriticalAlert;
    center.RequestAuthorization(options, (bool success, NSError error) => {
        // ...
    );
    return true;
}

Local critical alerts

To send a local critical alert, create a UNMutableNotificationContent and set its Sound property to either:

  • UNNotificationSound.DefaultCriticalSound, which uses the default critical notification sound.
  • UNNotificationSound.GetCriticalSound, which allows you to specify a custom sound that is bundled with your app and a volume.

Then, create a UNNotificationRequest from the notification content and add it to the notification center:

var content = new UNMutableNotificationContent()
{
    Title = "Critical alert title",
    Body = "Text of the critical alert",
    CategoryIdentifier = "my-critical-alert-category",
    // Sound = UNNotificationSound.DefaultCriticalSound
    Sound = UNNotificationSound.GetCriticalSound("my_critical_sound.m4a", 1.0f)
};

var request = UNNotificationRequest.FromIdentifier(
    Guid.NewGuid().ToString(),
    content,
    UNTimeIntervalNotificationTrigger.CreateTrigger(3, false)
);

var center = UNUserNotificationCenter.Current;
center.AddNotificationRequest(request, null);

Important

Critical alerts will not be delivered if they are not enabled for your app. Along with the prompt that appears the first time an app requests permission to send critical alerts, a user can also enable or disable critical alerts in your app's Notifications section of the iOS Settings app.

Remote critical alerts

For information about remote critical alerts, please see the What's New In User Notifications session from WWDC 2018 and the Generating a Remote Notification document.