Set up-to-date badge count when using UNNotificationRequest with trigger

hi, I am trying to schedule a UNNotificationRequest at a certain date using UNCalendarNotificationTrigger, and I also want to update the badge count accordingly. However the badge property in UNNotificationContent can only be set when adding UNNotificationRequest, not when the trigger is fired or notification is actually delivered. How can I set up-to-date badge count if notification is scheduled in the future?

For example, if I scheduled notification A to 3 hours later with badge count 1, and in between I got notification B, badge count should be 2 when A is delivered but it will be 1.

I am aware of didPresent delegate which can be used when app is in foreground when notification is delivered, but is there any delegate that is called when UNNotificationRequest is delivered and app is backgrounded or we are using NSE? Thanks.

There really isn't a straightforward way to accomplish what you are trying to do. Depending on your exact use case and conditions, this might be possible to do with some code gymnastics.

In general, you need to keep the internal state for the badge count yourself.

NSE is only available for push notifications, so it cannot be used for local notifications.

Which begs the question, in your example case, is notification B a push notification?

If all we are dealing with is local notifications, then you can manage the badge count in the app. For example, you set Notification A with a badge count 1. Then you are going to set notification B. As your app would (could) be aware of further pending notification A (and knows that the badge count is changing), as it schedules Notification B, it can also go delete the future Notification A with a badge count 1, and reschedule it with a badge count 2. And if a Notification C gets scheduled before A is triggered, then repeat the same to change Notification A to have badge number 3, and on and on...

Even if the app's flow is slightly different, at any point you are aware of the badge number changing, and effecting any future notifications, you can delete and reschedule new notifications with the changed badge number. This is possible because your app is executing at the time you are aware of this change.

If, in your example case, Notification B is a push notification, then you can use a Notification Service Extension (NSE) to do the same. After you set Notification A with a badge number of 1, a push notification arrives that will increase the badge count, in the NSE, you can do the same as above, knowing that Notification A is pending with a now stale badge number, delete Notification A, and reschedule it with an updated badge number. And if Notification C arrives before A is triggered, repeat the same, and on and on...

If you are not aware, you can get the undelivered pending notifications using getPendingNotificationRequests


Argun Tekant /  DTS Engineer / Core Technologies

Set up-to-date badge count when using UNNotificationRequest with trigger
 
 
Q