在 Swift 3 中接收远程通知

Receiving a Remote Notification in Swift 3

我有以下代码:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    let queryNotification = CKQueryNotification(fromRemoteNotificationDictionary: userInfo)
    let notification = Notification(name: NSNotification.Name("Name"), object: nil, userInfo: ["Key": queryNotification])
    NotificationCenter.default.post(notification)
}

但是,有人告诉我这不是接收远程通知的正确方式。相反,我被指示使用以下委托方法。我看不出如何使用此方法来完成我上面所做的事情。有人请帮忙。

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

我想你想做的是这样的:

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

    // Extrapolate userInfo
    let userInfo = response.notification.request.content.userInfo
    let queryNotification = CKQueryNotification(fromRemoteNotificationDictionary: userInfo)
    let notification = Notification(name: NSNotification.Name("Name"), object: nil, userInfo: ["Key": queryNotification])
    NotificationCenter.default.post(notification)

    completionHandler()
}