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.
6 Answers
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
-
1This 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.– JasarienFeb 1, 2011 at 10:06
-
2In iOS 8.0 and later, your application must register for user notifications using -[UIApplication registerUserNotificationSettings:] before being able to set the icon badge.– unomAug 24, 2016 at 13:12
And for everyone using new and shiny Swift:
UIApplication.sharedApplication().applicationIconBadgeNumber = someNumber
Swift 3:
UIApplication.shared.applicationIconBadgeNumber = someNumber
-
2when suggesting swift answers it is a good practice to include which version are you covering, since it is very dynamic right now. 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
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.
-
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.– JustSidFeb 1, 2011 at 10:46
-
Set UIApplication
's applicationIconBadgeNumber
property in your code when application is running:
[UIApplication sharedApplication].applicationIconBadgeNumber = someNumber;
For Objective C you have to use:
[[UIApplication sharedApplication] setApplicationIconBadgeNumber : anyNumber ];