2017-02-05 296 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 –