Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment the Push notification Badge iPhone

People also ask

What is iOS notification badge count?

The iOS badge count displays the number of unread notifications within your application, taking the form of a red circle in the upper-right hand corner of the app icon. In recent years, badging has come to be an effective means for re-engaging app users.

What are Badges in iPhone notification settings?

Badges are the red little circles or numbers that appear over an app icon on your Home Screen, telling you how many unread notifications you have for that app.

How do you get email Badges on iPhone?

Go to Settings > Mail > Notifications, then tap Customize Notifications. Tap the email account. Select the setting you want, like Alerts or Badges. When you turn on Alerts, you'll see an option to customize your Sounds by changing the alert tone or ringtone.


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.


If the application is not open you will not be able to increase the badge except from the payload.


When a Push Notification comes while your application is in background mode & you want to increment the Badge Number, you should send a badgeCount to the server, so that the server knows the current count.

If you manage the badge count from the Server Side then this code is enough:-

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo  {
    NSLog(@"remote notification: %@",[userInfo description]);

    if (userInfo) {
        NSLog(@"%@",userInfo);

        if ([userInfo objectForKey:@"aps"]) { 
            if([[userInfo objectForKey:@"aps"] objectForKey:@"badgecount"]) {
                [UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey: @"badgecount"] intValue];
            }
        }
    }
}

Urban Airship supports this using their "autobadge" feature.