1

I am using UNUserNotification to push repeated local notifications to the user. I am trying to set badge number on the app to indicate the missed notifications. I searched through this forum and found iPhone: Incrementing the application badge through a local notification

This says that it is not possible for recurring local notification. That post is couple of years old and they were using UILocal notification.

is this still true for UNUserNotification? Is there no way to set badge count?

user2092512
  • 435
  • 4
  • 12

1 Answers1

0

Set the badge property of a UNMutableNotificationContent object.

let notificationContent = UNMutableNotificationContent()
notificationContent.badge = 3

From the UNMutableNotificationContent documentation:

badge

The number to display as the app’s icon badge.

nathangitter
  • 9,607
  • 3
  • 33
  • 42
  • Thank you for the response Nathan. I was able to set the badge number as you mentioned. But i am using recurring local notification, i.e my local notification gets triggered say every 60 min, i see the same notification coming again and again with the same badge number. Do you have any suggestion on how to set the badge number for recurring notifications? – user2092512 Sep 07 '17 at 21:05
  • You'll probably need your own logic to record the number of notifications. An easy solution is to save a counter value to `UserDefaults`. Increment this counter when you send a new notification, and reset it to 0 when the user opens the app (or whatever is required to view the notifications). – nathangitter Sep 07 '17 at 21:13
  • Since i am using recurring notification like this UNTimeIntervalNotificationTrigger(timeInterval: 3600, repeats: true). I do not have control on every notification that is sent out, at least i am not aware of how to control each notification. – user2092512 Sep 07 '17 at 21:37
  • I don't think this is currently possible without using remote notifications. – nathangitter Sep 07 '17 at 21:47
  • There may be a way, but it would have limitations. Instead of using `repeats: true`, send out some number of notifications, each with an appropriate badge number (1, 2, 3, ...). When the application is launched, you can cancel all future notifications, and reset the notifications with new badge numbers. The limitation of this approach is it will have an upper limit (the number of notifications you schedule for the future), meaning that it will stop incrementing after a certain point. – nathangitter Sep 07 '17 at 21:50
  • Will there be any other complication in cancelling the notifications and adding them all again except upper limit? – user2092512 Sep 07 '17 at 22:21
  • Probably. It's a workaround and I've never done it before, but it's an idea! – nathangitter Sep 07 '17 at 22:46