0

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.

AjLearning
  • 379
  • 5
  • 16

2 Answers2

4

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.

NSGangster
  • 2,397
  • 12
  • 22
  • Thanks @NSGangster. How does `ReceivedRemoteNotification` fit into all this? – AjLearning Oct 25 '18 at 02:06
  • It's deprecated. Don't use it unless you are supporting versions previous to iOS 10.0 – NSGangster Oct 26 '18 at 17:55
  • Ok I just noticed where the deprecation is noted. It's in the Syntax example on this page: https://developer.xamarin.com/api/member/MonoTouch.UIKit.UIApplicationDelegate.ReceivedRemoteNotification/p/MonoTouch.UIKit.UIApplication/MonoTouch.Foundation.NSDictionary/. I was looking for a _Deprecated_ sign in big noticeable red font on that page, my bad. – AjLearning Oct 29 '18 at 00:02
2

application:didReceiveRemoteNotification:fetchCompletionHandler: is NOT deprecated. It's still required to handle background notifications (the ones that wake up your app to do stuff in the background).

UNUserNotificationCenter mostly deals with UI-related notifications or actions, but still cannot handle background notification.

aswierczek
  • 83
  • 4
  • This is correct. I misused the word deprecated in my post. I meant the usual method of receiving notification related data had changed. – NSGangster Jan 25 '21 at 18:29