【问题标题】:UNUserNotificationCenterDelegate not called XCode SwiftUNUserNotificationCenterDelegate 不叫 XCode Swift
【发布时间】:2020-09-28 16:34:40
【问题描述】:

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

这是我的 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。
我在后台收到通知,但不是在前台。

谢谢。

【问题讨论】:

    标签: ios swift uilocalnotification localnotification


    【解决方案1】:

    请试试这个

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

    【讨论】:

      猜你喜欢
      • 2017-03-02
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-19
      • 2016-11-08
      相关资源
      最近更新 更多