How to increment application badge number for recurring local notification (iPhone)

16,725

Solution 1

After doing lot's of research I figured out the solution is that there is no solution:

iPhone: Incrementing the application badge through a local notification

It is not possible to update dynamically the badge number with local notifications while your app is in the background. You have to use push notifications.

Solution 2

If you use an outside service such as Parse for Push, this should be easily done. Just increment Parses badge number when a local notification is fired. Although, this is a special case.

Share:
16,725
Admin
Author by

Admin

Updated on June 07, 2022

Comments

  • Admin
    Admin about 2 years

    I've setup a local notification that repeats every minute, however I need the application badge number to increment each time. When I run it at the moment it doesn't seem to increase, it just stays a 1. Please can someone help me out?

    Here is how I create the notifications:

    // Create the UILocalNotification
    UILocalNotification *myNotification = [[UILocalNotification alloc] init];
    myNotification.alertBody = @"Blah blah blah...";
    myNotification.alertAction = @"Blah";
    myNotification.soundName = UILocalNotificationDefaultSoundName;
    myNotification.applicationIconBadgeNumber++;
    myNotification.timeZone = [NSTimeZone defaultTimeZone];
    myNotification.repeatInterval = NSMinuteCalendarUnit;
    myNotification.fireDate = [[NSDate date] dateByAddingTimeInterval:30];
    [[UIApplication sharedApplication] scheduleLocalNotification:myNotification];
    
  • Admin
    Admin over 12 years
    Unfortunately this didn't work for me - thanks for the suggestion though.
  • BFar
    BFar over 10 years
    While this answer is technically correct, you can actually accomplish what The Crazy Chimp wants to do with a little extra work using the shared UIApplication instance. Every time that you schedule a new local notification or the app loads, you can use the 'scheduledLocalNotifications' property and the 'cancelAllLocalNotifications' property to cancel all future notifications and reassign badge counts to future local notifications by enumerating through them in chronological order & incrementing the badge number you assign. Def not as easy as typing applicationBadgeNumber++, but it works.
  • Tiago A.
    Tiago A. over 9 years
    +1 to this user's comment. I tested this approach and it works. Here goes a more complete explanation of the implementation: stackoverflow.com/a/15461328/2546416
  • Hugues BR
    Hugues BR over 8 years
    yep but that won't work if you're using repeatInterval.. if you're app is never started again and you can't predict badge value (because you're having a daily repeat interval) then you can't increment...
  • dev_m
    dev_m about 8 years
    it missing updateBadgeCountsForQueuedNotifiations method implementation ... please update it ...