我建议阅读苹果发布的 iOS 10+用户通知的新设计模式
过去的远程通知总是由UIApplicationDelegate
协议/接口处理,并且由于您的AppDelegate
类默认实现该协议,因此通常的做法是处理从那里传入的远程通知,而现在没有那么多使用
application:didReceiveRemoteNotification:fetchCompletionHandler:
.
在 iOS 10+ 中,Apple 将通知抽象到 UNUserNotificationCenter
框架中,为了分配委托,您必须将其分配给 UNUserNotificationCenter.Current.Delegate
子类的自定义类,UserNotificationCenterDelegate
或者像我一样,让您AppDelegate
实现接口并在那里处理事情。
这是我将如何实现的方法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.
}
}
当然,如果您不熟悉这些类,您将需要查看User Notifications框架以了解如何实现诸如UNNotification
和响应之类的类。UNNotification