Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将本地通知标记计数增加到1以上_Ios_Swift_Xcode_Notifications_Swiftui - Fatal编程技术网

Ios 将本地通知标记计数增加到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"

我目前正试图在我的应用程序中实现本地通知,但由于某些原因,无法将徽章计数器增加到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 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指定了在发送通知时增加应用程序的标记号的数字。我不知道为什么它在设计上不能像这样工作。但只有当应用程序在发送通知时运行时,它才会工作。当本地通知在应用程序关闭时发送时,是否有办法做到这一点?