UNUserNotificationCenterDelegate不称为XCode Swift

问题描述

在iOS中(Swift)接收本地通知时,不会调用

UNUserNotificationCenterDelegate 函数。另外,我无法在前台接收通知

这是我的AppDelegate类


    
    let notificationCenter = UNUserNotificationCenter.current()

    func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        DBManager.createEditablecopyOfDatabaseIfNeeded()
        notificationCenter.delegate = self
        let options: UNAuthorizationoptions = [.alert,.sound,.badge]
        notificationCenter.requestAuthorization(options: options) {
            (didAllow,error) in
            if !didAllow {
                print("User has declined notifications")
            }
        }
        return true
    }
}

UNUserNotificationCenterDelegate的应用程序委托扩展


    func userNotificationCenter(_ center: UNUserNotificationCenter,willPresent notification: UNNotification,withCompletionHandler completionHandler: @escaping (UNNotificationPresentationoptions) -> Void) {
        completionHandler([.alert,.sound])
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter,didReceive response: UNNotificationResponse,withCompletionHandler completionHandler: @escaping () -> Void) {
        print(response.notification.request.content.userInfo)
        completionHandler()
    }

    func scheduleNotification(_ body: String,_ date:Date) {
        let identifier = self.convertDateInMiliseconds(date: date)
        let content = UNMutableNotificationContent()
        content.title = "TEST APP"
        content.body = body
        content.sound = UNNotificationSound.default
        content.badge = 1
        content.categoryIdentifier = "\(identifier)1234"
        let triggerWeekly = Calendar.current.dateComponents([.weekday,.hour,.minute,.second,],from: date)
        let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly,repeats: true)

        let request = UNNotificationRequest(identifier: "\(identifier)",content: content,trigger: trigger)
        UNUserNotificationCenter.current().add(request) { (error) in
            if error == nil {
                print("TRUE")
            } else {
                print("FALSE")
                print(error?.localizedDescription ?? "ERROR")
            }
        }
        let snoozeAction = UNNotificationAction(identifier: "Snooze",title: "Snooze",options: [])
        let deleteAction = UNNotificationAction(identifier: "DeleteAction",title: "Delete",options: [.destructive])
        let category = UNNotificationCategory(identifier: "\(identifier)1234",actions: [snoozeAction,deleteAction],intentIdentifiers: [],options: [])

        notificationCenter.setNotificationCategories([category])
    }

    func convertDateInMiliseconds(date: Date) -> Int {
        let since1970 = date.timeIntervalSince1970
        return Int(since1970 * 1000)
    }

}

我正在使用iOS 14,swift 5.0和xcode 11.5。
我在后台收到通知,但没有在前台收到通知

谢谢。

解决方法

请尝试这个

func scheduleNotification(_ body: String,_ date:Date) {
    let identifier = self.convertDateInMiliseconds(date: date)
    let content = UNMutableNotificationContent()
    content.title = "TEST APP"
    content.body = body
    content.sound = UNNotificationSound.default
    content.badge = 1
    content.categoryIdentifier = "\(identifier)1234"
    let triggerWeekly = Calendar.current.dateComponents([.weekday,.hour,.minute,.second,],from: date)
    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerWeekly,repeats: true)

    let center = UNUserNotificationCenter.current()
    center.delegate = self

    let request = UNNotificationRequest(identifier: "\(identifier)",content: content,trigger: trigger)
    UNUserNotificationCenter.current().add(request) { (error) in
        if error == nil {
            print("TRUE")
        } else {
            print("FALSE")
            print(error?.localizedDescription ?? "ERROR")
        }
    }
    let snoozeAction = UNNotificationAction(identifier: "Snooze",title: "Snooze",options: [])
    let deleteAction = UNNotificationAction(identifier: "DeleteAction",title: "Delete",options: [.destructive])
    let category = UNNotificationCategory(identifier: "\(identifier)1234",actions: [snoozeAction,deleteAction],intentIdentifiers: [],options: [])

    notificationCenter.setNotificationCategories([category])
}