Share via


Notification management in Xamarin.iOS

In iOS 12, the operating system can deep link from Notification Center and the Settings app to an app's notification management screen. This screen should allow users to opt in and out of the various types of notifications the app sends.

Notification management screen

In the sample app, ManageNotificationsViewController defines a user interface that allows users to independently enable and disable red notifications and green notifications. It is a standard UIViewController containing a UISwitch for each notification type. Toggling the switch for either type of notification saves, in user defaults, the user's preference for that type of notification:

partial void HandleRedNotificationsSwitchValueChange(UISwitch sender)
{
    NSUserDefaults.StandardUserDefaults.SetBool(sender.On, RedNotificationsEnabledKey);
}

Note

The notification management screen also checks whether or not the user has completely disabled notifications for the app. If so, it hides the toggles for the individual notification types. To do this, the notification management screen:

  • Calls UNUserNotificationCenter.Current.GetNotificationSettingsAsync and examines the AuthorizationStatus property.
  • Hides the toggles for the individual notification types if notifications have been completely disabled for the app.
  • Re-checks whether notifications have been disabled each time the application moves to the foreground, since the user can enable/disable notifications in iOS Settings at any time.

The sample app's ViewController class, which sends the notifications, check's the user's preference before sending a local notification to ensure that the notification is of a type the user actually wants to receive:

partial void HandleTapRedNotificationButton(UIButton sender)
{
    bool redEnabled = NSUserDefaults.StandardUserDefaults.BoolForKey(ManageNotificationsViewController.RedNotificationsEnabledKey);
    if (redEnabled)
    {
        // ...

iOS deep links to an app's notification management screen from Notification Center and the app's notification settings in the Settings app. To facilitate this, an app must:

  • Indicate that a notification management screen is available by passing UNAuthorizationOptions.ProvidesAppNotificationSettings to the app's notification authorization request.
  • Implement the OpenSettings method from IUNUserNotificationCenterDelegate.

Authorization request

To indicate to the operating system that a notification management screen is available, an app should pass the UNAuthorizationOptions.ProvidesAppNotificationSettings option (along with any other notification delivery options it needs) to the RequestAuthorization method on the UNUserNotificationCenter.

For example, in the sample app's AppDelegate:

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
    // Request authorization to send notifications
    UNUserNotificationCenter center = UNUserNotificationCenter.Current;
    var options = UNAuthorizationOptions.ProvidesAppNotificationSettings | UNAuthorizationOptions.Alert | UNAuthorizationOptions.Sound | UNAuthorizationOptions.Provisional;
    center.RequestAuthorization(options, (bool success, NSError error) =>
    {
        // ...

OpenSettings method

The OpenSettings method, called by the system to deep link to an app's notification management screen, should navigate the user directly to that screen.

In the sample app, this method performs the segue to the ManageNotificationsViewController if necessary:

[Export("userNotificationCenter:openSettingsForNotification:")]
public void OpenSettings(UNUserNotificationCenter center, UNNotification notification)
{
    var navigationController = Window.RootViewController as UINavigationController;
    if (navigationController != null)
    {
        var currentViewController = navigationController.VisibleViewController;
        if (currentViewController is ViewController)
        {
            currentViewController.PerformSegue(ManageNotificationsViewController.ShowManageNotificationsSegue, this);
        }

    }
}