Is it possible to do this? UIApplication's
scheduledLocalNotifications
doesn't seem to return notifications that have already been delivered to the user's notification center, so I think this may be by design, but I can't find any documented evidence of this.
Anyone know?
Thanks!
EDIT: Found this:
You can cancel a specific scheduled notification by calling cancelLocalNotification: on the application object, and you can cancel all scheduled notifications by calling cancelAllLocalNotifications. Both of these methods also programmatically dismiss a currently
Here: http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html
However, how do I get a reference to an already-delivered notification, if scheduledLocalNotifications
doesn't give me notifications that have already been delivered?
EDIT 2:
Here's what I'm trying to do, after I've registered some notifications:
UIApplication *app = [UIApplication sharedApplication];
for (UILocalNotification *localNotification in app.scheduledLocalNotifications)
{
if (someCondition) {
[app cancelLocalNotification:localNotification];
}
}
}
The problem is that once they're delivered, they're no longer in 'scheduledLocalNotifications'.
You can solve this by adding your newly created notifications to your own NSMutableArray
of notifications and check that array instead of app.scheduledLocalNotifications
.
Something like this:
Add a NSMutableArray
to your Viewcontrollers .h file:
NSMutableArray *currentNotifications;
Initiate it when initiating your ViewController
currentNotifications = [[NSMutableArray alloc] init];
When initiating a notification, also add it to your array:
UILocalNotification *notification = [[UILocalNotification alloc] init];
...
[currentNotifications addObject:notification];
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
Later when you want to cancel that notification, look for it in your array instead. Also remove it from your array:
for (UILocalNotification *notification in currentNotifications) {
if (someCondition) {
[[UIApplication sharedApplication] cancelLocalNotification:notification];
[currentNotifications removeObject:notification];
}
}
Since iOS10 there is now native support for this if you have transitioned to using UNUserNotificationCenter
.
The Apple docs state:
func getDeliveredNotifications(completionHandler: @escaping ([UNNotification]) -> Void)
Provides you with a list of the app’s notifications that are still displayed in Notification Center.
func removeDeliveredNotifications(withIdentifiers: [String])
Removes the specified notifications from Notification Center.
func removeAllDeliveredNotifications()
Removes all of the app’s notifications from Notification Center.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With