Removing UILocalNotification from notification tray programmatically

喜夏-厌秋 提交于 2019-11-30 05:17:07

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];

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

[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];

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];

// 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];
  }
}

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");
    }
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!