11

I want to clear all push notifications of my application, once user selects one of the push notification.

I have seen other threads here which says it's not possible in iOS. but I have one application downloaded from app store, which does the same thing.

Baby Groot
  • 4,637
  • 39
  • 52
  • 71
Vishal Kardode
  • 961
  • 2
  • 8
  • 25
  • 1
    You might consider posting some more info about the app that has this feature. – Richard Stelling Aug 05 '13 at 12:11
  • And also links where it said to be impossible. This way your question would be more informative – Tala Aug 05 '13 at 12:12
  • 2
    just set your app's badge count as 0. it will clear all notifications from your notification center – Irshad Mansuri Aug 05 '13 at 12:12
  • @rjstelling Application is "WhatsApp", popular app for mobile chatting in India – Vishal Kardode Aug 05 '13 at 12:45
  • possible duplicate of [Removing a notification from notification center on click](http://stackoverflow.com/questions/9017348/removing-a-notification-from-notification-center-on-click) – bummi Aug 05 '13 at 13:53
  • http://stackoverflow.com/questions/8682051/ios-application-how-to-clear-notifications –  Sep 30 '13 at 11:41

2 Answers2

28

If it is a local notification then to remove badge icon you have to do it like this

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.applicationIconBadgeNumber = 1;

If it is push notification the you can do it by code written below

[UIApplication sharedApplication].applicationIconBadgeNumber=0;

You may call these methods anywhere you want. For example if you want to clear notification at the moment when the app is launched then write it in

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

if your app doesn't use the badge number you have to first set, then reset it to remove it from notification centre.

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
Vaibhav Gautam
  • 2,074
  • 17
  • 17
2

All you need to do is

application.applicationIconBadgeNumber = 0;

in

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

EDIT

If you are not closing your app but just sending it to background. Then add this in your below function as well.

  - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground  )
    {
         //opened from a push notification when the app was on background
         application.applicationIconBadgeNumber = 0;
    }
}
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
  • 1
    Yeah that what i said.. and if your application is in background mode then check in this delegate method - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { } – Irshad Mansuri Aug 05 '13 at 12:31
  • As per my understanding, application.applicationIconBadgeNumber = 0; will reset Badge icon, not the notifications in notification centre. I did gave it a try , but did not work – Vishal Kardode Aug 05 '13 at 12:41
  • Well. Here notification has been deleted from notification center whenever i am opening particular app with notification. – Baby Groot Aug 05 '13 at 12:46