Ios 将本地通知标记计数增加到1以上
我目前正试图在我的应用程序中实现本地通知,但由于某些原因,无法将徽章计数器增加到1以上 下面是我配置和安排通知的方法Ios 将本地通知标记计数增加到1以上,ios,swift,xcode,notifications,swiftui,Ios,Swift,Xcode,Notifications,Swiftui,我目前正试图在我的应用程序中实现本地通知,但由于某些原因,无法将徽章计数器增加到1以上 下面是我配置和安排通知的方法 func scheduleNotification() { let content = UNMutableNotificationContent() content.title = "\(self.title == "" ? "Title" : self.title) is done" content.subtitle = "Tap to view"
func scheduleNotification() {
let content = UNMutableNotificationContent()
content.title = "\(self.title == "" ? "Title" : self.title) is done"
content.subtitle = "Tap to view"
content.sound = UNNotificationSound.default
content.badge = 1
if self.isPaused {
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: self.currentTime, repeats: false)
let request = UNNotificationRequest(identifier: self.notificationIdentifier.uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
} else {
removeNotification()
}
}
由于某些原因,当成功计划并确实传递多个通知时,无论实际传递的通知数是多少,徽章计数器最多只增加1
func scheduleNotification() {
let content = UNMutableNotificationContent()
content.title = "\(self.title == "" ? "Title" : self.title) is done"
content.subtitle = "Tap to view"
content.sound = UNNotificationSound.default
content.badge = 1
if self.isPaused {
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: self.currentTime, repeats: false)
let request = UNNotificationRequest(identifier: self.notificationIdentifier.uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
} else {
removeNotification()
}
}
是否有一种适当的方法来管理徽章计数,而这不是它?您应该考虑代码的作用。您不是在增加徽章数量,而是每次将其设置为1 以下是实现徽章计数的一种方法:
func scheduleNotifications(notificationBody: String, notificationID: String) {
//Your other notification scheduling code here...
//Retreive the value from User Defaults and increase it by 1
let badgeCount = userDefaults.value(forKey: "NotificationBadgeCount") as! Int + 1
//Save the new value to User Defaults
userDefaults.set(badgeCount, forKey: "NotificationBadgeCount")
//Set the value as the current badge count
content.badge = badgeCount as NSNumber
}
应用程序中(uquot:didFinishLaunchingWithOptions:)
方法在应用程序启动时将徽章计数重置为零:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.applicationIconBadgeNumber = 0
userDefaults.set(0, forKey: "NotificationBadgeCount")
}
目前也在处理这个问题,不知道该怎么办。谁来帮忙!非常感谢。我刚刚计算出content.badge指定了在发送通知时增加应用程序的标记号的数字。我不知道为什么它在设计上不能像这样工作。但只有当应用程序在发送通知时运行时,它才会工作。当本地通知在应用程序关闭时发送时,是否有办法做到这一点?