Search code examples
iosbadgelocalnotification

LocalNotification badge shows wrong Number in iOS7


I have built a test-application for local notifications. it looks like this

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
    localNotification.alertBody = @"My Next Custom Notification";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
    
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];
    localNotification.alertBody = @"Second One Now";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

    NSLog(@"Notifications: %@", [[UIApplication sharedApplication] scheduledLocalNotifications]);

My AppDelegate:

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

It works fine when I'm in Background or in the LockScreen I'm getting these notifications.

But when the first notifications comes in, it does not show the badge which should be "1" now. When the Second notification comes in the the badge shows "1" and not "2"(which would be the right number now). So what is wrong with my code?


Solution

  • You are making a simple mistake that you are scheduling the notification first than setting its badge number. So you should first set the notification badge number then schedule it.

    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
        localNotification.alertBody = @"My Next Custom Notification";
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    
        localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];
        localNotification.alertBody = @"Second One Now";
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        localNotification.applicationIconBadgeNumber = localNotification.applicationIconBadgeNumber + 1;
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    NSLog(@"Notifications: %@", [[UIApplication sharedApplication] scheduledLocalNotifications]);
    

    Also for the right badge number during the second notification either you should save your notification badge number or set it from the last notification badge as I had did in above code. Because in your code it just set the badge number from the current [UIApplication sharedApplication] badge which is 0 + 1 equals 1 each time.