Request Notifications Permission in a .Net MAUI App for IOS


This worked on my Hot Restart-deployed IOS app: in Platforms/iOS/AppDelegate.cs, use the following code derived from Dima Ordenov’s MauiSamples

using UIKit;
using UserNotifications;

...

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
    RegisterForRemoteNotifications(application);

    return base.FinishedLaunching(application, launchOptions);
}

/// <summary>
/// Reqeust permission for notifications
/// </summary>
/// <param name="application"></param>
/// <remarks>from https://github.com/DimaOrdenov/MauiSamples/blob/main/MauiPushNotifications/Platforms/iOS/AppDelegate.cs</remarks>
private void RegisterForRemoteNotifications(UIApplication application)
{
    UNUserNotificationCenter.Current.RequestAuthorization(
        UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
        (isSuccess, error) =>
        {
            if (error != null)
            {
                // no op, not going to do anything about this for now
            }
        });

    application.RegisterForRemoteNotifications();
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img