elsurudo elsurudo - 4 years ago 136
iOS Question

Dismiss an already delivered UILocalNotification?

Is it possible to do this?

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?


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
doesn't give me notifications that have already been delivered?


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

Answer Source

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];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download