Removing UILocalNotification from notification tray programmatically
Solution 1
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];
Solution 2
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
Solution 3
[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];
Solution 4
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];
Solution 5
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");
}
}
Mr.Anonymous
Updated on June 13, 2022Comments
-
Mr.Anonymous about 2 years
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. -
strange about 10 yearsThis does not work if the notification has already displayed as 'scheduledLocalNotifications' will return zero entries (since it's no longer 'scheduled' and has already fired). Any other way to remove a notification when tapped?
-
mars about 7 yearsgrt! thnx. It wrkd.