AppDelegate 方法仅在应用程序已打开时从通知中调用

AppDelegate method is only called from a notification when the app is already open

我正在尝试让推送通知在按下时将用户带到特定的视图控制器。

目前我的 AppDelegate 中有这个:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
        if let error = error {
            print(error.localizedDescription)
        } else {
            application.registerForRemoteNotifications()
        }
    }
    
    UNUserNotificationCenter.current().delegate = self

    return true
}

当我按下通知并从后台打开应用程序时,none 个被调用:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    
    completionHandler([.alert, .sound, .badge])
}

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

}

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

}

如果您的应用不在后台 运行,您将在 didFinishLaunchingWithOptions 中像这样处理通知

let notificationInfo = launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] as? [String : Any]

如果您的应用在 background/foreground 中 运行,您将在

中处理通知
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)