本地通知未在Swift 4中调用didReceiveRemoteNotifications

时间:2018-08-03 06:04:36

标签: ios swift apple-push-notifications swift4 ios11

我有以下appDelegate:

import UIKit
import CoreData
import UserNotifications

@available(iOS 11.0, *)
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
{
    var window: UIWindow?

    ...

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any])
    {
        print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    }
}

我正在使用ios11和swift 4.1,但通知已正确触发。但是当我点击它时,我无法在应用程序中抓住它。为什么?我正在使用xcode 9.4.1。谢谢。

该应用程序处于后台,点击通知后,该应用程序将打开。

1 个答案:

答案 0 :(得分:2)

您好@cdub您应该使用usernotificatioCenter

 func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    // do code here to show notification alert in for-ground mode 
}




func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    //your notification handling code here 
    }