0

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

我目前在我的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
}

当我按下通知并从后台打开应用程序时,这些都不会被调用:

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) {

}
4

1 回答 1

0

如果您的应用程序没有在后台运行,您将didFinishLaunchingWithOptions像这样处理通知

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

如果您的应用程序在后台/前台运行,您将处理通知点击

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
于 2020-10-10T08:47:58.957 回答