Would you believe it, not a single result when I search for this. The Xamarin APIs on developer.xamarin don't mention any relationship between the two either.
And to make things even more complicated, developer.apple says DidReceiveRemoteNotification
is deprecated but there's no mention of this deprecation on developer.xamarin. Moreover, https://learn.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-xamarin-ios-get-started-push guides you on using it.
So now there's WillPresentNotification
in the mix as well.
Can someone please shed some light on these? Specifically, how they are related, and which one to use when.
I recommend reading up on the new design pattern apple has post iOS 10+ for User Notifications
The old days remote notifications were always handled by the UIApplicationDelegate
protocol/interface and since your AppDelegate
class defaults to implement that protocol it was common practice to handle incoming remote notifications from there with the now not used as much
application:didReceiveRemoteNotification:fetchCompletionHandler:
.
In iOS 10+, Apple abstracted notifications into the UNUserNotificationCenter
framework
and in order to assign the delegate you must assign the UNUserNotificationCenter.Current.Delegate
to either a custom class that sublcasses from UserNotificationCenterDelegate
or, like me, make your AppDelegate
implement the interface and handle things there.
Here is how I would implement things in Xamarin.iOS
:
using Foundation
using UIKit;
using UserNotifications;
public class AppDelegate : UIApplicationDelegate, IUNUserNotificationCenterDelegate
{
public override UIWindow Window { get; set; }
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
UNUserNotificationCenter.Current.Delegate = this;
return true;
}
[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, System.Action completionHandler)
{
//Handle Notification if user interacts with notification in Notification Center of iOS.
}
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, System.Action<UNNotificationPresentationOptions> completionHandler)
{
//Handle Notification if app is in the foreground when recieved.
}
}
Of course you will want to look at the User Notifications framework to see how to implement classes such as UNNotification
and UNNotification
response if you are not familiar with those classes.