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.

Juan Boero
  • 6,281
  • 1
  • 44
  • 62
fabian789
  • 8,348
  • 4
  • 45
  • 91
  • Same question, but for macOS: https://stackoverflow.com/questions/392797/how-do-i-draw-a-badge-on-my-dock-icon-using-cocoa – Jon Schneider Jun 11 '21 at 21:03

6 Answers6

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
Mohsin Khubaib Ahmed
  • 1,008
  • 16
  • 32
raaz
  • 12,410
  • 22
  • 64
  • 81
  • 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 01 '11 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 '16 at 13:12
17

And for everyone using new and shiny Swift:

UIApplication.sharedApplication().applicationIconBadgeNumber = someNumber

Swift 3:

UIApplication.shared.applicationIconBadgeNumber = someNumber
Patrick Tescher
  • 3,387
  • 1
  • 18
  • 31
Jiri Trecak
  • 5,092
  • 26
  • 37
  • 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 09 '15 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 :) – Jiri Trecak Dec 09 '15 at 21:49
  • 3
    Jiri, amusing comment in hindsight ;-) – Tim Nov 01 '17 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.

JustSid
  • 25,168
  • 7
  • 79
  • 97
  • 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 01 '11 at 10:46
  • Ok, that's what I wanted to know. Thanks! – fabian789 Feb 01 '11 at 12:23
7

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

[UIApplication sharedApplication].applicationIconBadgeNumber = someNumber;
Vladimir
  • 170,431
  • 36
  • 387
  • 313
0

For Objective C you have to use:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber : anyNumber ];            
S. S
  • 169
  • 3
  • 16
Vaibhav Shiledar
  • 939
  • 8
  • 15
0

Swift 5

UIApplication.shared.applicationIconBadgeNumber = someNumber
Ben
  • 3,346
  • 6
  • 32
  • 51