0

I want to update application badge icon when a notification is received. It works fine when app is running (active mode),but having trouble when trying to set it when app is in suspended/background mode. Want to achieve the same without using the 'badge' field in apns dictionary.

Here is what i am doing,

-(void) incrementOneBadge{
NSInteger numberOfBadges = [UIApplication sharedApplication].applicationIconBadgeNumber;
numberOfBadges +=1;

  [[UIApplication sharedApplication] setApplicationIconBadgeNumber:numberOfBadges];

}

-(void) decrementOneBadge{ NSInteger numberOfBadges = [UIApplication sharedApplication].applicationIconBadgeNumber;
numberOfBadges -=1;

  [[UIApplication sharedApplication] setApplicationIconBadgeNumber:numberOfBadges];
}

-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  UIApplicationState state = [application applicationState];
  if (state == UIApplicationStateActive) {

  } 
  else {
    // Push Notification received in the background
    [self incrementOneBadge];

  }

  [self handlePushNotificationsForState:application withDictionary:userInfo];
}
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
Shantanu
  • 3,086
  • 3
  • 25
  • 32
  • You need to add badge info in push notification `Payload`, it will automatically increment in all state of application, you don't need to do it programatically. – iphonic Mar 21 '15 at 05:52
  • @iphonic: I dont want to do it through Payload (want to handle it in the application, no logic on server). – Shantanu Mar 21 '15 at 05:55
  • then its not possible to handle in Suspend mode, other than push notification. – iphonic Mar 21 '15 at 05:56
  • It is managed from server side to give badge icon, not from ios side. http://stackoverflow.com/questions/23931484/application-badge-icon-not-updated-when-push-notification-arrives – Max Mar 21 '15 at 06:08

0 Answers0