44

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; 
}
Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • 1
    you can just send the new count as payload, to make sure the correct count will display when the application is in background and foreground – janusfidel Jun 22 '12 at 09:34

5 Answers5

55

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.

Mathew Varghese
  • 4,527
  • 2
  • 17
  • 26
  • 6
    Shouldn't the key to get the badge count out of the aps dict be @"badge"? See: http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40006786-CH3-SW16 – qix Sep 19 '12 at 08:08
  • application:didReceiveRemoteNotification: will this method get called even the app is killed? – jailani Jul 04 '14 at 11:55
  • 2
    @Mathew Varghese, each user will have different unread notifications, how badge count will be accurate by keeping count on server side? – subha Apr 25 '16 at 11:18
  • In case you broadcast a message to many devices, you can't set the badge. How can you increase badge count in this case? – Anh Nguyen Jun 28 '22 at 04:00
  • @Mathew Varghese: could you reply on the comments please? – mAc Aug 29 '23 at 14:19
15

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

Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
6

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];
            }
        }
    }
}
staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
Deepesh
  • 8,065
  • 3
  • 28
  • 45
  • BUt how could i send the badge count to the server??@elppa –  Jun 22 '12 at 10:18
  • @moosa,server send push notification in your device when server side kbown your device token and server mange your badge count through your device token id, i think no need to send the badge count to the server – Deepesh Jun 22 '12 at 10:52
  • So in the payload if the badge valur is send as 1 always, will it get incremented in the app if it is not opened?? –  Jun 22 '12 at 11:41
  • No, When app is open then increment – Deepesh Jun 22 '12 at 11:52
  • Deepesh, you should review your answer since there is a few things wrong with it. E.g. Parenthesis typos and also you are assigning the application badge number to the boolean value of [[[userInfo objectForKey:@"aps"] allKeys] containsObject:@"alert"] – Groot May 24 '13 at 07:46
3

Urban Airship supports this using their "autobadge" feature.

Jagat Dave
  • 1,643
  • 3
  • 23
  • 30
Matt Frear
  • 52,283
  • 12
  • 78
  • 86
  • 1
    Per the link, `autobadge is an Urban Airship feature to track badge numbers in our servers`, so the payload must just include a badge number that is one greater than the previous push. – JRG-Developer Jan 08 '14 at 23:46
  • 1
    Nope. Read the link - you can send "+1" and Urban Airship will increment it for you. – Matt Frear Jan 09 '14 at 14:30
  • 1
    ... Urban Airship maintains the count for you on *their servers*, they set the badge number in the payload for you... it's not just "+1" in the payload (it's whatever number should be set), push notifications don't have any notion of "+1" badge number, AFAIK. – JRG-Developer Jan 09 '14 at 21:15
-1

After receiving remote Notification when you open App,

get current Badge number in "didBecomeActive" Method of your Appdelegate File using below code:

int badgeCount = [UIApplication sharedApplication].applicationIconBadgeNumber;
    badgeCount = badgeCount + 1;
Vikas S Singh
  • 1,748
  • 1
  • 14
  • 29
nidIOS
  • 167
  • 1
  • 9