Updating iOS badge without push notifications
Asked Answered
M

6

89

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.

Marylouisemaryly answered 1/2, 2011 at 9:57 Comment(1)
Same question, but for macOS: #393297Closemouthed
A
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
Airla answered 1/2, 2011 at 10:2 Comment(2)
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.Gantrisin
In iOS 8.0 and later, your application must register for user notifications using -[UIApplication registerUserNotificationSettings:] before being able to set the icon badge.Rheingold
A
17

And for everyone using new and shiny Swift:

UIApplication.sharedApplication().applicationIconBadgeNumber = someNumber

Swift 3:

UIApplication.shared.applicationIconBadgeNumber = someNumber
Alphaalphabet answered 4/8, 2015 at 13:40 Comment(3)
when suggesting swift answers it is a good practice to include which version are you covering, since it is very dynamic right now.Amphipod
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 :)Alphaalphabet
Jiri, amusing comment in hindsight ;-)Roband
N
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.

Nitz answered 1/2, 2011 at 10:11 Comment(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.Nitz
Ok, that's what I wanted to know. Thanks!Marylouisemaryly
T
7

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

[UIApplication sharedApplication].applicationIconBadgeNumber = someNumber;
Tea answered 1/2, 2011 at 10:2 Comment(0)
F
0

For Objective C you have to use:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber : anyNumber ];            
Flathead answered 27/7, 2016 at 11:48 Comment(0)
E
0

Swift 5

UIApplication.shared.applicationIconBadgeNumber = someNumber
Engelhart answered 20/7, 2020 at 22:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.