is it possible to increment the application badge through a local notification while the app is not running?
I know how to set the badge, but haven't found any way to increment this value.
localNotification.applicationIconBadgeNumber = 23;
Update: I found a (far from being perfect) solution. You can predict what will happen, if the user doesn't open the app and add notifications for every +1 event.
An example:
- For day 1: Count = 0
- For day 2: localNotification.applicationIconBadgeNumber = 1;
- For day 3: localNotification.applicationIconBadgeNumber = 2;
- For day 4: localNotification.applicationIconBadgeNumber = 3;
==> Put these notifications in an array and set them before the application exits.
However, I'm searching for a better solution than this workaround.
Best Answer
I've found, implemented & tested a 'workaround' for (apparantly) auto-incrementing the app icon's badge number, that works fine with non-repeating local notifications
It is indeed not possible for UILocalNotifications to have iOS 'automatically' update/increment the badge number when multiple local notifications are fired, and the user 'ignores' them or doesn't handle them immediately, so they 'pile up' in the Notification centre.
Also 'adding some callback method' to your app cannot take care of the 'auto increment', because the whole notification thing is handled 'outside' of your app by iOS, your app doesn't even need to be running.
However there is some workaround, that is based on the knowledge which I found through experimenting, because the XCode documentation is too vague on the badge property.
So for a 'workaround' your app must already provide the correct, incrementing badge number for each notification it newly creates and registers 'on top of the pending notifications'.
Since your app cannot look in the future, and know which events you'll handle immediately, and which ones you'll leave 'pending' for a while, there's some trick to do :
When notifications are handled by your app (by tapping on the notification(s), icon, ...), you have to :
Also, when your app registers a new notification, it has to check how many notifications are pending first, and register the new notification with with :
Looking at my code, it will get clearer. I tested this, and it's definitely working :
In your 'registerLocalNotification' method you should do this :
When you handle the notification (appDelegate), you should call the method below, which clears the badge on the icon and renumbers the badges for pending notifications (if there are any)
Note that the next code works fine for 'sequential' registered events. If you would 'add' events in between pending ones, you'll have to 're-sort' these events first. I didn't go that far, but I think it's possible.
To be truly 'bullet proof', this method should be 'atomic' (kernel) code, preventing the iOS from firing of a notification during the execution of this method. We'll have to take this risk here, chances are very small this will happen.
This is my first contribution to Stackoverflow, so you can comment also if I'm not following the 'rules' here