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

Local notification not calling didReceiveRemoteNotifications in swift 4

我有以下 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。谢谢

应用程序在后台运行,当点击通知时应用程序打开。

你好@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 
    }