user782104 user782104 - 1 year ago 70
iOS Question

How to update the badge number in IOS push notification?

I recently use the Amazon SNS to push notification for my IOS app.

It works well, the only problem I have encountered is when I receive the notification , the badge number will not be updated, here is how I implement:

First I follow the example here
Here is the example code from the tutorial.

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
application.applicationIconBadgeNumber = 0;
NSString *msg = [NSString stringWithFormat:@"%@", userInfo];
[[Constants universalAlertsWithTitle:@"Push Notification Received" andMessage:msg] show];

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//Register for push notification
application.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
NSString *msg = [NSString stringWithFormat:@"%@", launchOptions];
[[Constants universalAlertsWithTitle:@"Push Notification Received" andMessage:msg] show];

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

// Override point for customization after application launch.

Message_BoardViewController *boardViewController = [Message_BoardViewController new];
UINavigationController *navigationController = [UINavigationController new];

navigationController.navigationBar.translucent = NO;

[navigationController pushViewController:boardViewController animated:NO];
[boardViewController release];

self.window.rootViewController = navigationController;
[navigationController release];

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];

[self.window makeKeyAndVisible];

// Logging Control - Do NOT use logging for non-development builds.
#ifdef DEBUG
[AmazonLogger verboseLogging];
[AmazonLogger turnLoggingOff];

[AmazonErrorHandler shouldNotThrowExceptions];

return YES;

As you can see, from the tutorial code it state

application.applicationIconBadgeNumber = 0;

so obviously it will become 0 every time.


I wonder what is the standard way to update the badge number?

Which appoarch is correct ?

1) Through programming like this
application.applicationIconBadgeNumber = 0;

2) Or from the server payload like this?

$body = array(
'alert' => $message,
'sound' => 'sound.caf',
'badge' => $badge_count // Should be int type


However, I found there is obstacle for each apporach, for the 1), the
is not fire when the app is at background , so I can not do something like
to update the badge number.

for the 2) , the Amazon SNS service is only return

$body = array(
'alert' => $message,

and simply how can the server knows the badge number and add it at server payload, it seems I still need to post the update badge number to Amazon in
and add it to payload. But again, it does not call at background.

Sorry for new to IOS progamming, would some experience programmer kindly guide me to implement the badge number update with push notification? Thanks.


You are supposed to send the badge number in the payload of the push notification. Your server should set the badge count. This way the badge count will be updated regardless of whether the app is running or not running (and regardless of whether the user taps the notification to open the app or not). Your server should know what number to send.

And as to how your server is supposed to know what badge number to send - I don't know your app's logic, but lets take an email application as an example - suppose you want the badge number to show how many unread messages exist. The app should notify the server whenever a message is read, so the server can maintain the correct badge number for each user. This way, even if the user has multiple ways to access the data (iPhone app, iPad app, desktop browser, etc...), the server would know the current badge count and each device would show the correct badge count.

The application.applicationIconBadgeNumber = 0 is meant to clear the badge once the application is opened. It should be done in both didReceiveRemoteNotification and didFinishLaunchingWithOptions, so that the badge number will always be cleared when the application is launched, or when the push notification is handled by the app.