title: How to update the badge number in IOS push notification?
body: <p>I recently use the Amazon SNS to push notification for my IOS app.</p>
  
  <p>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:</p>
  
  <p>First I follow the example here
  <a href="https://aws.amazon.com/articles/9156883257507082" rel="nofollow">https://aws.amazon.com/articles/9156883257507082</a>
  Here is the example code from the tutorial.</p>
  
  <pre><code>    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
      application.applicationIconBadgeNumber = 0;
      NSString *msg = [NSString stringWithFormat:@"%@", userInfo];
      NSLog(@"%@",msg);
      [[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)];
      if(launchOptions!=nil){
          NSString *msg = [NSString stringWithFormat:@"%@", launchOptions];
          NSLog(@"%@",msg);
          [[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];
  #else
      [AmazonLogger turnLoggingOff];
  #endif
  
      [AmazonErrorHandler shouldNotThrowExceptions];
  
      return YES;
  }
  </code></pre>
  
  <p>As you can see, from the tutorial code it state</p>
  
  <pre><code>application.applicationIconBadgeNumber = 0;
  </code></pre>
  
  <p>so obviously it will become 0 every time.</p>
  
  <p>======================================================</p>
  
  <p><strong>I wonder what is the standard way to update the badge number</strong>? </p>
  
  <p>Which appoarch is correct ?</p>
  
  <p>1) Through programming like this <code>application.applicationIconBadgeNumber = 0;</code></p>
  
  <p>2) Or from the server payload like this?</p>
  
  <pre><code>    $body = array(
  'alert' =&gt; $message,
  'sound' =&gt; 'sound.caf',
  'badge' =&gt; $badge_count // Should be int type
  );
  </code></pre>
  
  <p>========================================================</p>
  
  <p><strong>However, I found there is obstacle for each apporach</strong>, for the 1), the <code>didReceiveNotification</code> is not fire when the app is at background , so I can not do something like <code>application.applicationIconBadgeNumber++;</code> to update the badge number.</p>
  
  <p>for the 2) , the Amazon SNS service is only return </p>
  
  <pre><code>$body = array(
  'alert' =&gt; $message,
  ); 
  </code></pre>
  
  <p>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 <code>didReceiveNotification</code> and add it to payload. But again, it does not call at background.</p>
  
  <p>Sorry for new to IOS progamming, would some experience programmer kindly guide me to implement the badge number update with push notification? Thanks.</p>