88

I have seen a few todo apps that update their app badges at midnight, always showing the correct number of due tasks. They do this without the use of Push Notifications - so my question is: how do they do this? Do they use local notifications - if so, do these get called when the device is turned off? I'm a little confused and would appreciate some input.

1

6 Answers 6

138

Try this

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];

To do this through local notifications you have to set the value in applicationIconBadgeNumber

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.applicationIconBadgeNumber = 1;// set here the value of badge
2
  • 1
    This is how you change the badge number - but in order to do it while the app is in the background or if the phone is asleep, you may need to look into local notifications, or scheduled tasks.
    – Jasarien
    Feb 1, 2011 at 10:06
  • 2
    In iOS 8.0 and later, your application must register for user notifications using -[UIApplication registerUserNotificationSettings:] before being able to set the icon badge.
    – unom
    Aug 24, 2016 at 13:12
17

And for everyone using new and shiny Swift:

UIApplication.sharedApplication().applicationIconBadgeNumber = someNumber

Swift 3:

UIApplication.shared.applicationIconBadgeNumber = someNumber
3
  • 2
    when suggesting swift answers it is a good practice to include which version are you covering, since it is very dynamic right now.
    – Juan Boero
    Dec 9, 2015 at 21:48
  • yes you are right ; In this case however, there is nothing in the syntax that could possibly change (it is simple assignment) so probably not needed :) Dec 9, 2015 at 21:49
  • 3
    Jiri, amusing comment in hindsight ;-)
    – Tim
    Nov 1, 2017 at 14:41
13

Since iOS 4.0 you can fire local notifications on all devices that run at least iOS 4.0. Look into the UILocalNotification class, it allows you to set the badge at midnight without having your app running.

2
  • Uhm, the iPhone isn't even capable to start the alarm clock when its powered off, so no, the badge won't update then. But when you turn it back on, it will fire the notification(s) that will update the badge.
    – JustSid
    Feb 1, 2011 at 10:46
  • Ok, that's what I wanted to know. Thanks!
    – fabian789
    Feb 1, 2011 at 12:23
7

Set UIApplication's applicationIconBadgeNumber property in your code when application is running:

[UIApplication sharedApplication].applicationIconBadgeNumber = someNumber;
0

For Objective C you have to use:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber : anyNumber ];            
0

Swift 5

UIApplication.shared.applicationIconBadgeNumber = someNumber

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.