未调用 UNUserNotificationCenterDelegate XCode Swift

UNUserNotificationCenterDelegate not called XCode Swift

UNUserNotificationCenterDelegate 函数在 ios (swift) 中接收本地通知时不会被调用。另外,我无法在前台接收通知。

这是我的 AppDelegate class


    
    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])
}