Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear Push notifications from notification centre after selecting one

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.

like image 362
Vishal Kardode Avatar asked Aug 05 '13 12:08

Vishal Kardode


2 Answers

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];
like image 94
Vaibhav Gautam Avatar answered Oct 17 '22 01:10

Vaibhav Gautam


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;
    }
}
like image 40
Baby Groot Avatar answered Oct 17 '22 00:10

Baby Groot