iOS 11 应该由 App Delegate 方法或仅由 UNDelegate 方法处理推送通知

Posted

技术标签:

【中文标题】iOS 11 应该由 App Delegate 方法或仅由 UNDelegate 方法处理推送通知【英文标题】:iOS 11 should push notifications be handled by App Delegate method or solely by UNDelegate methods 【发布时间】:2018-04-16 13:51:58 【问题描述】:

我一直在寻找一个直接的答案,但还没有遇到,所以我假设在这里问我的问题。对于 ios 10 后的应用,远程通知应该由

处理

应用委托方法:

 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) 


或者,UNDelegate 方法:

 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) 


【问题讨论】:

【参考方案1】:

TVOS 不支持UserNotificationsUI.framework,所以如果您需要支持 TVOS,请坚持使用原始的UIApplicationDelegate 方法。

否则,如果您可以坚持使用 iOS10 后的支持,UNUserNotificationCenter 是我的首选,因为将来可能会弃用 UIApplicationDelegate

如果您使用的是 WatchKit,didReceiveRemoteNotification 已弃用,请勿使用它。来自documentation:

不使用此方法,而是创建一个委托对象,该对象采用 UNUserNotificationCenterDelegate 协议并实现 userNotificationCenter:willPresentNotification:withCompletionHandler: 和 userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: 方法。将此对象分配给单例的委托属性 UNUserNotificationCenter 对象。

【讨论】:

以上是关于iOS 11 应该由 App Delegate 方法或仅由 UNDelegate 方法处理推送通知的主要内容,如果未能解决你的问题,请参考以下文章

QuickBlox iOS SDK,启用云后端服务器 AUTHKEY/APPLICATION ID TO APP DELEGATE

从 Xcode 11 更新到 12 后的 App Delegate/Scene Delegate 和 Firebase 问题 [关闭]

iOS单元测试如何阻止App Delegate做事

iOS [[UIApplication sharedApplication] delegate]运用

IOS 将值从 App Delegate 传递到初始视图控制器并在 viewWillAppear 中使用这些值

Google Sign In 调用 App Delegate 中的方法后关闭 View Controller (iOS / Swift)