Hello,
For iOS 10 and later, please see the User notification framework guide. (The doc is about Xamarin, it applies to MAUI)
First, Notification permission should be requested in the FinishedLaunching
method of the AppDelegate
and setting the desired notification type (UNAuthorizationOptions
), then set the UserNotificationCenterDelegate
. Please see the code snippets in Preparing for Notification Delivery part of the doc.
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
// Request notification permissions from the user
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert, (approved, err) => {
// Handle approval
});
UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate(); //set delegate
}
return base.FinishedLaunching(application, launchOptions);
}
Second, you can add UserNotificationCenterDelegate
to handle the foreground App notifications, please see the code snippets in the Handling Foreground App Notifications part of the doc.
public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]// pay attention to this line
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
completionHandler(UNNotificationPresentationOptions.Banner);
}
}
Then you can invoke platform code to send local notification for testing, I add a PushNotificationService
, you can refer to the following code:
public partial class PushNotificationService
{
public partial void PushLocalNotification();//Define the cross-platform API, which includes a partial method named PushLocalNotification. The code file for this class must be outside of the
}
And call the Add
method of the UNUserNotificationCenter
object to schedule its display to the user in this PushLocalNotification().
Please see the code snippets in Scheduling When a Notification is Sent part of the doc.
namespace iOSNotoficationSample.Services//the same namespace as PushNotificationService
{
public partial class PushNotificationService
{
public partial void PushLocalNotification()//implement the API on iOS platform.
{
var content = new UNMutableNotificationContent();
...//code snippets
});
}
}
}
After that, you can call the PushLocalNotification
method in MAUI
private void OnCounterClicked(object sender, EventArgs e)
{// click the button on MainPage to send a local notification for testing
#if IOS
PushNotificationService pushNotificationService = new PushNotificationService();
pushNotificationService.PushLocalNotification();
#endif
}
Besides, you can try Working with Notification Action
In addition, this solution is for Local Notification as described in the title if this thread. I saw you set "aps" in the code snippets, you may need Remote Notification. If so, you can create a new thread for Remote Notification. If you have any other issues, please feel free to post in here.
Best Regards,
Wenyan Zhang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.