在Swift中接收远程通知3

时间:2017-02-05 20:17:44

标签: ios swift notifications cloud cloudkit

我有以下代码:

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

1 个答案:

答案 0 :(得分:2)

我认为您尝试做的事情是这样的:

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()
}