Clear app badge with local notifications
Solution 1
I had the same problem. When setting the badge from a local notification, setting it to 0 is the default for 'no change', while doing it straight from the application would clear it. Setting it to a negative number through a local notification solved the problem.
try:
clearEpisodeNotification.applicationIconBadgeNumber = -1;
Solution 2
Yes, it is possible to clear the badge from the app itself.
I use the code below in one of my apps, and it works as expected (i.e. clears the badge):
//clear app badge
[UIApplication sharedApplication].applicationIconBadgeNumber=0;
peterjb
Updated on June 06, 2022Comments
-
peterjb about 2 years
I'm trying to clear my app's "unread" badge with a
UILocalNotification
. Logically you would think this would be done by settingapplicationIconBadgeNumber
of aUILocalNotification
instance to 0. But it doesn't work, and the docs for applicationIconBadgeNumber say "The default value is 0, which means "no change.”"So is there really no way to clear a badge with local notifications once it's been set?
Update: Some simple code:
-(void)applicationDidFinishLaunching { // Set the appication icon badge to 1 in 10 minutes, using a local notification so it works in the background: // This works fine. UILocalNotification *episodeNotification = [[UILocalNotification alloc] init]; episodeNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:(60 * 10)]; episodeNotification.timeZone = [NSTimeZone defaultTimeZone]; episodeNotification.applicationIconBadgeNumber = 1; [[UIApplication sharedApplication] scheduleLocalNotification:episodeNotification]; [episodeNotification release]; // Clear the application icon badge in 20 minutes, again using a local notifcation so it works in the background: // This doesn't work. According to the docs for local notification it's not supposed to // because (applicationIconBadgeNumber = 0) means "Do not change the badge" // I'm looking for an alternative if it exists. UILocalNotification *clearEpisodeNotification = [[UILocalNotification alloc] init]; clearEpisodeNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:(60 * 20)]; clearEpisodeNotification.timeZone = [NSTimeZone defaultTimeZone]; clearEpisodeNotification.applicationIconBadgeNumber = 0; [[UIApplication sharedApplication] scheduleLocalNotification:clearEpisodeNotification]; [clearEpisodeNotification release]; }
-
uvesten over 13 yearsAh, I see you want to do it using a local notification. Why don't you want do it from within the app itself?
-
peterjb over 13 yearsIt's an app that monitors live events it knows about by parsing an iCalendar, and updates its badge based on how many live events are running right now. If the number of live events falls to 0 while the user is out of the app, the badge should disappear.
-
peterjb about 13 yearsThank you! That's just what I needed.
-
Shashikant More over 8 yearsBoth answers solve the problem. But where did it stored? Even we delete the app and reinstall, it appears again.
-
TimChang over 5 years.applicationIconBadgeNumber = 0; It work in ios 10.0+ developer.apple.com/documentation/uikit/uiapplication/…