Devendra Singh Devendra Singh - 1 year ago 126
Objective-C Question

How to increase badge number when application is in background

I am using this code. Every thing is working fine when push notification comes but badge number does not increase when application is in background. How to solve this problem?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// Override point for customization after application launch.

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

//UIApplication *application = [UIApplication sharedApplication];
NSInteger badgeNumber = [application applicationIconBadgeNumber];// Take the current badge number
//badgeNumber--; // decrement by one
[application setApplicationIconBadgeNumber:[[[launchOptions valueForKey:@"aps"]valueForKey:@"badge"]integerValue]]; // set ne badge number

NSLog(@"userInfo :%@ %d",launchOptions,[[[launchOptions valueForKey:@"aps"]valueForKey:@"badge"]integerValue]);

return YES;

Answer Source

When the application is in the background didFinishLaunchingWithOptions method never calls. For doing something when your App is in background you need to implement your logic in AppDelegate's applicationDidEnterBackground: method like.

- (void)applicationDidEnterBackground:(UIApplication *)application{

[UIApplication sharedApplication].applicationIconBadgeNumber = 2;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download