xamarin.iosazure-pipelines.net-mauiunusernotificationcenter

.Net Maui iOS app with Push Notifications WillPresentNotification or DidReceiveNotificationResponse never gets called


I'm able to call RegisterForRemoteNotifications(); from the AppDelegate to get the Token. I'm then using an APN server I built to update Apple Passes. I'm sending a message with the correct payload, headers etc..., but nothing ever happens.

I'm assuming that something needs to be added to MauiProgram.cs in order to get this to work.

Seeing as I have my own APN server, I really don't want to use Firebase or Azure Notification Hub at this time. A Firebase method is provided in the following link.

[https://cedricgabrang.medium.com/firebase-push-notifications-in-net-maui-ios-2f4388bf1ac][1]

Seeing as Local Notifications and Remote Notifications use the same methods, I modeled my code after this example.

https://github.com/xamarin/xamarin-forms-samples/tree/main/LocalNotifications

Here is my code with some decorations.

        // Called if app is in the foreground.
        [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
        public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
        {
            SentrySdk.CaptureMessage("WillPresentNotification = " + "yes");

            ProcessNotification(notification);
            completionHandler(UNNotificationPresentationOptions.Alert);
        }

        // Called if app is in the background, or killed state.
        [Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
        public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
        {
            SentrySdk.CaptureMessage("DidReceiveNotificationResponse = " + "yes");

            if (response.IsDefaultAction)
            {
                ProcessNotification(response.Notification);
            }
            completionHandler();
        }

NOTE: I'm compiling my iOS app using an Azure DevOps Pipeline. I've added both the Development cert and the Push Notification cert to the Keychain. I don't see the Push Notification cert included in the DotNetCoreCLI@2 Task. Push Notifications are enabled and configured in the Provision Profile. Perhaps there is a problem with the build process and how the Keychain installs/uses the all the certs.

Does anyone know how to make this work? Seems like this should be pretty easy to do.


Solution

  • I finally got this working using the Firebase.plugin for .Net Maui. My APN server works. The Firebase console works too.

    I used these directions.

    https://cedricgabrang.medium.com/firebase-push-notifications-in-net-maui-ios-2f4388bf1ac

    If you are having issues getting Firebase.plugin to build on iOS, take a look at this issue. https://github.com/TobiasBuchholz/Plugin.Firebase/issues/51

    Here is the code I used in my Azure Pipeline to combine the two Apple Certs. https://stackoverflow.com/a/74105887/5360237