How to increase notification badge number dynamically when local notification is delivered?

My App sets up multiple local notifications via a loop based on a calendar date and time (calendar trigger). While everything else is working correctly with this, I cannot seem to get the badge number to increment properly. I tried setting this up via content.badge but what happens is...
  1. User gets notification 1,2,3,4,5 and badge number gets set to 5 content.badge = self.counter

  2. User then "clears" notifications 2 and 3 -> and badge number is now 3 via my code (UIApplication.shared.applicationIconBadgeNumber - 1)

  3. When the next notification (number 6) pops up, the badge number SHOULD show 4. It doesn't. It shows 6 because that's what was set via content.badge

What is the proper way to increment the badge number? I've looked at various posts here but they're for push notifications. Mine is for a local notifications. Can I set/update badge number WHEN a notification is actually delivered? ...Or does notification manager somehow handle this automatically?
Could you show the code where you set badge value ?

You should count yourself the notifications and apply to badge

Code Block
var openedNotifications = 0
myVC.myTabBarItem.badgeValue = String(openedNotifications)


Or you could store the notifications in an array, append when new notification, remove from the array when delivered and use .count
So, I'm running a loop that basically sets 50 notifications at a time. In the loop, I set the content,

Code Block
let content = self.notificationContent(title: "my title", body: "whatever else I want to say")
content.threadIdentifier = "my.thread"
content.categoryIdentifier = "my.category"
content.badge = self.counter as NSNumber  // <-- this counter gets incremented for each iteration of the loop
//.. Calendar trigger
var dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: myTime)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let identifier = "message.notify.reset\(self.notifyNumber)"
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
//.. Add it to notification center
UNUserNotificationCenter.current().add(request) { (error) in
      self.printError(error, location: "*** Add request for identifier: " + identifier)
 }


Then, once that one is added, I increment the counter and add the next one. It does all of this correctly but it's not really what I want to happen because once the notifications are set, it's basically out of my App... and whichever one triggers next, is the number that shows up for the badge. So, for example, if number 48 is the one that gets sent at 8pm, the badge now says 48 regardless of whether or not the user "cleared" any/all of the other notifications prior to that.

What I would like to see happen somehow is for the notification manager to recognize that when a new notification is delivered, it needs to look at the previous count for the badge number and then increment that by 1. So, if it was previously 7 for the badge number, it should be 8 after the next notification is delivered. Maybe it's me, but this seems kind of obvious/basic that that's the way it should work. Am I missing something? Is there a way to do this?
Can you specify on which case you think a new notification is delivered?
It is not obvious and it is you to define it.
Did you try to set a monitoring loop with a timer (a test every 5s for instance), and use
getDeliveredNotifications(completionHandler:)
to count delivered notifications ?

Simple timer example here:
https://stackoverflow.com/questions/57237412/run-repeating-function-in-swift
@Claude31 ::: Would this work with something like I'm doing? Remember, I set up all the notifications (50 of them) in my App while it is running. Then, the user exits (most likely) and just waits for the notifications to be delivered at their set calendar date/time. I know how to look at delivered notifications while the App is open but I'm not sure how I would code this in my UNUserNotificationCenterDelegate Class if I wanted to check this and then somehow have the correct badge number calculated (Note: UNUserNotificationCenterDelegate is where I can check to see how users interacted with the notifications and respond via custom actions that I set). If I tried a monitoring loop, wouldn't that only work if the App is open? Where in my code would I put that?
Timer don't work (in general) in background.

Have a look here
https://stackoverflow.com/questions/42319172/swift-3-how-to-make-timer-work-in-background
and more specifically here:
https ://www.raywenderlich. com/143128/background-modes-tutorial-getting-started
@Claude31 ::: Ok, thank you for the information. I will check this out and see what I can do. I just thought there would be a more automatic way of handling this since every App pretty much wants to increment the badge number by 1. I can decrement the badge number in my UNUserNotificationCenterDelegate Class via a custom "cancel" notification action... for example,

Code Block
UIApplication.shared.applicationIconBadgeNumber -= 1

... So, I thought that there was a way to add 1 to whatever is already delivered too.

How to increase notification badge number dynamically when local notification is delivered?
 
 
Q