Mr.Anonymous
Mr.Anonymous

Reputation: 820

Removing UILocalNotification from notification tray programmatically

Is there a way to programmatically remove/dismiss UILocalNotification from Notification Tray. I am able to cancel the notification which removes the notifications from

[[UIApplication sharedApplication] scheduledLocalNotifications]

Here is what i need to do

I need to dismiss the UILocalNotification from NotificationTray after the action has been performed(ie after the user taps the notification)

EDIT: I can remove the notifications from the NSNotificationCenter. I want to remove specific notifications from the Notification Tray .Like the user presses the clear button to clear all the notifications belonging to a particular application.

Upvotes: 12

Views: 11286

Answers (7)

Magyar Miklós
Magyar Miklós

Reputation: 4272

So I just read this thread about how to close/remove all the already fired local notifications from the Notification center, if the user opens the app by clicking the app icon, not the notification. But after all of this, the other scheduled local notification should fire in the future.

Here is my easy solution for this, which should be triggered on application did becomeActive:

UIApplication* application = [UIApplication sharedApplication];
NSArray* scheduledNotifications = [NSArray arrayWithArray:application.scheduledLocalNotifications];
application.scheduledLocalNotifications = scheduledNotifications;

I ve tried the [[UIApplication sharedApplication] cancelLocalNotification:notification]; but it did not clear the already fired local notifications from the Notification center (outside of the app).

Upvotes: 0

Midhun MP
Midhun MP

Reputation: 107231

You can cancel all notifications using:

[[UIApplication sharedApplication] cancelAllLocalNotifications];

If you want to remove a particular notification, you can use userinfo of notification object, when you create a local notification add a unique ID to that. Later you can use that ID for removing local notification.

For that you can use the following code:

NSString *notificationId = @"id_to_cancel";
UILocalNotification *notification = nil;
for(UILocalNotification *notify in [[UIApplication sharedApplication] scheduledLocalNotifications])
{
  if([[notify.userInfo objectForKey:@"ID"] isEqualToString:notificationId])
  {
     notification = notify;
     break;
  }
}
[[UIApplication sharedApplication] cancelLocalNotification:notification];

Upvotes: 16

p0lAris
p0lAris

Reputation: 4820

I was fiddling with some code and I was wondering why local notifications are stored in the notification center if the application is in the foreground. It's probably because Apple doesn't know what you are doing with them and honestly doesn't care; so they do their job.

As far as the question is concerned, I do the following:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    if (application.applicationState == UIApplicationStateActive)
    {
        NSLog(@"active");

        // display some foreground notification;
        [application cancelLocalNotification:notification];
    }
    else
    {
        NSLog(@"inactive");
    }
}

Upvotes: 1

mklb
mklb

Reputation: 1285

// deletes a pushnotification with userInfo[id] = id
-(void)deleteLocalPushNotificationWithId:(NSString*)id{
  for(UILocalNotification *notification in [[UIApplication sharedApplication] scheduledLocalNotifications]){
    if([[notification.userInfo objectForKey:@"id"] isEqualToString:id]){
        [[UIApplication sharedApplication] cancelLocalNotification:notification];
    }
  }
}

// deletes all fired pushnotifications
-(void)clearLocalPushNotifications{
  NSArray *activeNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];

  // Clear all notifications to remove old notifications
  [[UIApplication sharedApplication] cancelAllLocalNotifications];

  // Add back the still relevant notifications
  for (UILocalNotification *notification in activeNotifications) {
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
  }
}

Upvotes: 1

Anthony Mattox
Anthony Mattox

Reputation: 7118

I believe I had a similar issue. When the app entered the foreground I attempted to clear past notifications to remove any old notifications from the notifications tray.

I did something like this to grab old notifications and remove them:

NSArray *activeNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
NSArray *pastNotifications = [activeNotifications filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"firDate < %@", [NSDate date]]];
for (UILocalNotification *notification in pastNotifications) {
    [[UIApplication sharedApplication] cancelLocalNotification:notification];
}

However, it seems that scheduledLocalNotifications does not include locations whose fire date is already past even though they still appear in notification center.

Calling cancelAllLocalNotifications does seem to remove past notifications as well. So we can grab all the current notifications, cancel everything, and then add the ones we're still interested in back.

// Grab all the current notifications
NSArray *activeNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];

// Clear all notifications to remove old notifications
[[UIApplication sharedApplication] cancelAllLocalNotifications];

// Add back the still relevant notifications
for (UILocalNotification *notification in activeNotifications) {
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}

Additionally we can do some filtering of the notifications before adding them back if some are no longer needed, and we can grab the active notifications when the app becomes active, store them in an instance variable, and only add them back when the app moves to the background

Upvotes: 6

Sj.
Sj.

Reputation: 1724

If the Application is not running, you will be receiving the Local Notification object in the

-applicationDidFinishLaunchingWithOptions:

like:

UILocalNotification *localNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey];

or else you can get it in

  • (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

Now you can remove it from the Notification Center using

[[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel];

Upvotes: 1

Mujah Maskey
Mujah Maskey

Reputation: 8804

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;

will do some trick too

but if you didnot use applicationIconBadgeNumber, it will not work, so trick is set applicationIconBadgeNumber first :)

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];

Upvotes: 2

Related Questions