Ready to Level-Up?

This just in! If your rusty iOS skills could use some refining, or if you're just starting out and you want to level-up fast, consider signing up for our upcoming iOS Screencasts

New from iOS Rocket Surgery Swift Video Tutorials: The rapidly evolving Apple-iOS ecosystem has just been thrown into tumult with the recent introduction of the Swift Programming Language. Are you ready to finally try iOS development? Are you needing to switch from Objective-C to Swift? Sign up today for our free video courses!
(By the dear folks at Swift Tutorial Videos, a sister company)

Wednesday, October 23, 2013

Clearing the iOS Notification Badge

So, you want to clear the iOS notification badge, eh?

Whether your backend app is pushing notification to your device (iPhone/iPad/Mac) or you're using a third party like Urban Airship to push them (smart decision probably since push notification certificates are a PITA), you'll want to clear your notifications badge when your app is opened by your user. In order to do this, you should setup a method called "clearNotificationBadge" in a utilities class somewhere.

I have a class called Utilities.h that I put useful functions like this in, and I suggest doing the same. Just put this method in your own Utilities.h file that you create for yourself:

+(void) clearNotificationBadge{
  // The badge won't update unless it changes so we have to
  // set it to a different value first before we clear it
  // out. Like this...
  [[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
  [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

Then, just invoke your new method in the applicationDidBecomeActive method of your AppDelegate.m file like so:

- (void)applicationDidBecomeActive:(UIApplication *)application
  // Restart any tasks that were paused (or not yet started)
  // while the application was inactive. If the application
  // was previously in the background, optionally refresh the
  // user interface.
  // Clear any notifications
  [Utilities clearNotificationBadge];

Boom, now you're cleaning up the messages like a pro. It feels dirty, but only because you're working with an API that seems buggy. What's really going on is that APPL is trying to be efficient on items that cross the sandbox barrier onto your device. They don't want to have to update that UI unless values are changing. So you have to do this set/reset song and dance. Its unfortunate, but easy to overcome.

Now go, and do great things worthy of honor! ;)

About Me

Easy NSDateFormatter Tool

Save yourself some time in formatting your NSDates to NSStrings, and use the Blind NSDate app, which you can download from iTunes. There's also a website where you can format your NSDates:

Popular Posts

Designed By Seo Blogger Templates