Search code examples
iosnotificationsapple-push-notificationsnotificationcenter

Removing a notification from notification center on click


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.


Solution

  • 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.