Increment the Push notification Badge iPhone

user825841 picture user825841 · Jun 22, 2012 · Viewed 69.9k times · Source

Is it possible to increment the badge value on receiving the notification. OR Should I send the count as payload?

If i am sending the badge value as "1" every time, how could i increment the badge-value in the icon of the app if the app is not open.

i have used this code but doesn't work.

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [UIApplication sharedApplication].applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1; 
}

Answer

Mathew Varghese picture Mathew Varghese · Jun 22, 2012

Usually in all apps the unread notification counts are maintained in the server. When the server sends a push notification to a particular device token they send the badge count along with the payload. Once the device is notified and your app is in background(or killed) the OS automatically update the badge count to your app icon. In case whether you have your app running, you will get notified in the

application:didReceiveRemoteNotification:

delegate and thus you are able to receive the badge count from the (NSDictionary *)userInfo. And thus you are able to update the app icon badge count using the function

[UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey: @"badgecount"] intValue];

Think this should help you.