Removing a notification from notification center on click

Andy Davies picture Andy Davies · Jan 26, 2012 · Viewed 22.6k times · Source

Is it possible to remove the push notification from the notification center when one is clicked and the app launches?

Most apps seem to leave the notification in place. I read on another question that this:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0]; 

may work however it doesn't work for me.

The Facebook app definitely seems to remove the push notifications once clicked on.

Answer

Andy Davies picture Andy Davies · Jan 27, 2012
int badgeCount = [UIApplication sharedApplication].applicationIconBadgeNumber;
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeCount];

If you add this to both

- (void)applicationWillEnterForeground:(UIApplication *)application

And

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions (NSDictionary *)launchOptions

You will retain the badge count, and clear the push notification when it is clicked in the notification center.