5

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.

mars
  • 1,651
  • 2
  • 15
  • 20

2 Answers2

4

You should not need to set the badge manually upon receiving a remote notification as iOS will set it automatically for you using the value in aps -> badge. This will work out of the box whether your app is active, inactive, in background or not even running (killed).

However iOS doesn't use the value to increment the badge, but to set it. So if your badge value is n in the push notification, regardless of the current badge value of your application, iOS will set it to n (not current + n). All the applications where the badge apparently increments when receiving a notification actually manage the badge count on their server and send the appropriate incremented value in the notification.

If thats the behavior that you want, this is what you should do. Typically you have a badgeCount property associated with each device on your server and you increment it every time you send a push. Then when the user opens your app you send a reset call to your server that resets this value to 0, and subsequent notifications will start back at 1.

deadbeef
  • 5,409
  • 2
  • 17
  • 47
  • got ur point but could'nt understand last para. I mean how would the server know that i have already read previous two notifications and it has to send 1 as badge value and not 3. – mars Sep 15 '15 at 11:27
0

Badge count is managed by iOS itself it is totally dependent on OS when app is in background or killed state.

The server side will handle the count and do the calculation.

Please take a look at this link:

"Update badge with push notification while app in background"

Community
  • 1
  • 1
Shubham Narang
  • 514
  • 2
  • 14