Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dismiss an already delivered UILocalNotification?

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

like image 814
elsurudo Avatar asked May 18 '12 12:05

elsurudo


2 Answers

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];
    }
}
like image 158
reekris Avatar answered Sep 25 '22 20:09

reekris


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.

like image 45
Justyn Avatar answered Sep 26 '22 20:09

Justyn