Reputation: 1679
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?
Upvotes: 0
Views: 297
Reputation: 4909
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.
Upvotes: 1
Reputation: 4036
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 = 1; //first notification
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];
localNotification.alertBody = @"Second One Now";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
localNotification.applicationIconBadgeNumber = 2; // second notification
NSLog(@"Notifications: %@", [[UIApplication sharedApplication] scheduledLocalNotifications]);
after you add condition check first is fire then cancelallnotification
and register remaining with decreasing badge order.
Upvotes: 0