0

你会相信吗,当我搜索这个时,没有一个结果。developer.xamarin 上的 Xamarin API 也没有提及两者之间的任何关系。

更复杂的是,developer.apple 说DidReceiveRemoteNotification已弃用,但在 developer.xamarin 上没有提及此弃用。此外,https: //docs.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-xamarin-ios-get-started-push 会指导您使用它。

所以现在也有WillPresentNotification混合。

有人可以阐明这些吗?具体来说,它们是如何相关的,以及何时使用哪一个。

4

2 回答 2

4

我建议阅读苹果发布的 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

于 2018-10-18T18:44:28.207 回答
2

application:didReceiveRemoteNotification:fetchCompletionHandler:不被弃用。仍然需要处理后台通知(唤醒您的应用程序在后台执行操作的通知)。

UNUserNotificationCenter 主要处理 UI 相关的通知或操作,但仍然无法处理后台通知。

于 2019-07-12T07:29:03.660 回答