Forum Xamarin.Mac

UNUserNotificationCenter in VS4M (v8.8.4.30 - Latest stable) authorisation always fails in Mac app

CartBlancheCartBlanche Member ✭✭

UNUserNotificationCenter in VS4M (v8.8.4.30 - Latest stable) authorization always fails in Mac app, while Swift version works. Example code below....

C# Code...

uses UserNotifications;

    public override void DidFinishLaunching (NSNotification notification)
            {
                   // Check we're at least v10.14
                    if (NSProcessInfo.ProcessInfo.IsOperatingSystemAtLeastVersion (new NSOperatingSystemVersion (10, 14, 0))) {
                        var currentNotificationCenter = UNUserNotificationCenter.Current;
                        currentNotificationCenter.Delegate = new MyUNUserNotificationCenterDelegate ();

                        // Request Permissions
                        currentNotificationCenter.RequestAuthorization (UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound, (granted, error) => {
                            if(error != null) {
                                NSApplication.SharedApplication.InvokeOnMainThread (() => {
                                    ShowAlert ("Notification Request Error", error.LocalizedDescription + Environment.NewLine + "Reason: " + error.LocalizedFailureReason);
                                });
                            }
                        });
                    }
            }

        internal class MyUNUserNotificationCenterDelegate : UNUserNotificationCenterDelegate
        {
        }

the error variable is always "not null" and the LocalizedDescription property reads "Notifications are not allowed for this application".

When I do the same within a Swift application, I get the Authorisation pop-up asking me to authorise Notifications....

Swift Code that works....

     import UserNotifications

         struct ContentView: View {
             var body: some View {
                 Text("Test UNNotification Authorization")
                     .padding(10)
                     .onAppear {
                         if #available(OSX 10.14, *) {
                             let currentUserNotificationCenter =
                             UNUserNotificationCenter.current()

                             let myDelegate = MyUserNotificationCenterDelegate()
                             currentUserNotificationCenter.delegate = myDelegate // must have delegate, otherwise notification won't appear
                             currentUserNotificationCenter
                               .requestAuthorization(options: [.alert, .sound, .badge]) {
                                 [weak myDelegate] granted, error in

                                 print("Permission granted: \(granted)")
                                 guard granted else { return }
                               .}
                         }
                     .}

     class MyUserNotificationCenterDelegate: NSObject, UNUserNotificationCenterDelegate {
     }

The authorisation pop up, never appears in my VS4M application.

Best Answer

  • CartBlancheCartBlanche Member ✭✭
    Accepted Answer

    The solution: With MacOS apps, for the permission dialog to appear, you MUST sign your MacOS app, even if building locally in DEBUG mode. So Project->Options->Mac Signing->Sign the application bundle must be ticked.

Answers

Sign In or Register to comment.