2017-02-05 38 views
-1

我有下面的代码接收远程通知:斯威夫特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) { } 
+0

你是正确的。远程通知未发送至UNNotificationCenterDelegate方法 – Paulw11

+0

@ Paulw11 - 对于iOS 10及更高版本,您必须使用UNNotificationCenterDelegate。 'UIUserNotifications'已被弃用。 – Pierce

+0

这对于请求通知权限和处理本地通知是正确的,但是你会看到'application(_ application:UIApplication,didReceiveRemoteNotification userInfo:[AnyHashable:Any],fetchCompletionHandler completionHandler:@escaping(UIBackgroundFetchResult) - > Void)'是*未弃用* https://developer.apple.com/reference/uikit/uiapplicationdelegate/1623013-application此外,“接收远程通知”未列在“UNNotificationCenter”概述中https://developer.apple.com/ reference/usernotifications/unusernotificationcenter?language = objc – Paulw11

回答

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

yep thanks thats it –