After going through almost all the tutorials I am still not able to set my badges right. Situation is like that- When my app is in background i want my badge to get increased by 1. So I am using below line of code
NSLog(@"In didReceiveRemoteNotification badge number is %ld",(long)[UIApplication sharedApplication].applicationIconBadgeNumber);
[UIApplication sharedApplication].applicationIconBadgeNumber += [[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] integerValue];
NSLog(@"In didReceiveRemoteNotification badge number is %ld",(long)[UIApplication sharedApplication].applicationIconBadgeNumber);
These two NSLogs gives me values 1 and 2 respectively. How even before setting applicationIconBadgeNumber it gets a 1? And therefore it shows 2 as badge on receiving single notification.
To make my question more clear I am posting whole code done in - didReceiveRemoteNotification
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler{
if ((application.applicationState == UIApplicationStateBackground)) {
NSLog(@"===========================");
NSLog(@"App was in BACKGROUND...");
NSLog(@"In didReceiveRemoteNotification badge number is %ld",(long)[UIApplication sharedApplication].applicationIconBadgeNumber);
[UIApplication sharedApplication].applicationIconBadgeNumber += [[[userInfo objectForKey:@"aps"] objectForKey:@"badge"] integerValue];
NSLog(@"In didReceiveRemoteNotification badge number is %ld",(long)[UIApplication sharedApplication].applicationIconBadgeNumber);
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotif];
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];}
if (application.applicationState == UIApplicationStateActive) {
NSLog(@"===========================");
NSLog(@"App was ACTIVE");
NSString *str = [[userInfo objectForKey:@"aps"] objectForKey:@"alert"];
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Notification Received" message:str delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
alertview.tag = 600;
[alertview show];
}
NSLog(@"%@",userInfo);
handler(UIBackgroundFetchResultNewData);}
My second question is when my app is killed i go for below code in didFinishLaunchingWithOptions:
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (localNotif) {
[UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + [[[localNotif valueForKey:@"aps"] valueForKey:@"badge"] integerValue];}
At first it didnt work at all but after reading an answer at Stackoverflow i removed UIRemoteNotificationTypeNewsstandContentAvailability and it started showing up. But still no increment was shown. Badge value remained 1.
Any help would be appreciated.