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.
Updating iOS badge without push notifications
Asked Answered
Same question, but for macOS: #393297 –
Closemouthed
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
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
And for everyone using new and shiny Swift:
UIApplication.sharedApplication().applicationIconBadgeNumber = someNumber
Swift 3:
UIApplication.shared.applicationIconBadgeNumber = someNumber
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
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. –
Nitz
Ok, that's what I wanted to know. Thanks! –
Marylouisemaryly
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 ];
Swift 5
UIApplication.shared.applicationIconBadgeNumber = someNumber
© 2022 - 2024 — McMap. All rights reserved.