首页 > 解决方案 > 本地通知中的徽章

问题描述

当我注册新通知时 - UIApplication.shared.applicationIconBadgeNumber - 总是 0,这就是为什么我的徽章中总是“1”。如何在本地通知中更新它?

 let content = UNMutableNotificationContent()
    let notificationInterval = components(forDay: settings.object.timeInterval)
    let requestIdentifier = settings.object.identifier
    content.badge = Swift.min(notificationsCount, UIApplication.shared.applicationIconBadgeNumber + 1) as NSNumber
    content.body = settings.object.body
    content.title = settings.object.title
    content.categoryIdentifier = "categoryIdentifier"
    content.sound = UNNotificationSound.default()
    content.setValue(true, forKey: "shouldAlwaysAlertWhileAppIsForeground")
    let trigger = UNCalendarNotificationTrigger(dateMatching: notificationInterval, repeats: true)
    let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
    UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [request.identifier])
    UNUserNotificationCenter.current().add(request) { error in
        if error != nil {
            print(error?.localizedDescription ?? "Notification error")
        }
        print("Notification Register Success")
    }

标签: swiftnotificationslocalbadge

解决方案


推荐阅读