Mike
Mike

Reputation: 118

MAUI .Net8 iOS DidReceiveRemoteNotification never invoked

I'm writing a .Net8 MAUI App targeting iOS. The application should be able to received remote APNS notifications. This works fine while the application is in foreground. The method WillPresentNotification is then invoked and the notification processed.

What I can't get to work is processing notifications while in background, without the user clicking on the notification. In other words, I'd like my application to process the notification payload, even if the user doesn't click on the notification.

I understand that the method DidReceiveRemoteNotification should be invoked (... according to other articles I've found, like this one. The difference is that I'm using .Net8 instead of .Net7. I've tried to modify my csproj file as mentioned in that article, not working for me.

The C# code of the methods I'm trying to trigger looks like this:

[Export("application:didReceiveRemoteNotification:fetchCompletionHandler:")]
public void DidReceiveRemoteNotification(UIApplication application, NSDictionary notification, Action<UIBackgroundFetchResult> completionHandler)
        {
            //code to write the notification to the DB and display the local notification

            completionHandler(UIBackgroundFetchResult.NewData);
        }

[Foundation.Export("application:didReceiveRemoteNotification:")]
public void ReceivedRemoteNotification(UIKit.UIApplication application, Foundation.NSDictionary userInfo)
        {
            ProcessNotification(userInfo, false);

        }

Interestingly enough, this method is not defined in UNUserNotificationCenterDelegate or in the MauiUIApplicationDelegate interfaces, so I can't actually override it.

Has anybody managed to handle notifications while in background on iOS using MAUI and .Net8?

Upvotes: 0

Views: 677

Answers (1)

zTim
zTim

Reputation: 78

I had the same problem

You need to check in your Aps definition that the property "ContentAvailable = true" is set when you send a Firebase message. This tells your app that the event is also called in the background.

var aps = new Aps()
{
    CustomData = iosNotificationDataObject,
    Sound = "default",
    ContentAvailable = true, //Tells the mobile app content is available
};

According to RemoteNotifications Programming content-available definition is

Provide this key with a value of 1 to indicate that new content is available. Including this key and value means that when your app is launched in the background or resumed, application:didReceiveRemoteNotification:fetchCompletionHandler: is called.

(Newsstand apps are guaranteed to be able to receive at least one push with this key per 24-hour window.)

Similiar: stackoverflow answer

Hope this helps :)

Upvotes: 0

Related Questions