Dismiss an already delivered UILocalNotification?

后端 未结 6 1424
无人及你
无人及你 2020-12-20 14:40

Is it possible to do this? UIApplication\'s scheduledLocalNotifications doesn\'t seem to return notifications that have already been delivered to t

相关标签:
6条回答
  • 2020-12-20 15:10

    You can do it when you schedule the notification if you also save it in NSUserDefaults, using NSKeyedArchiver to convert it to NSData.

    To get it back as UILocalNotification you use NSKeyedUnarchiver. Then you're able to delete it using the cancelLocalNotification method.

    Fully explained here (Swift version + link to original Obj-C solution)

    0 讨论(0)
  • 2020-12-20 15:15

    I've been looking for an answer to this as well. My problem was that I wanted to 'clean up' all app-related notifications sitting in the notification center just in case the user opens the app from its dock icon (since that does nothing to the previously fired notifications...)

    Fortunately, it would appear that cancelAllNotifications doesn't just cancel scheduled ones, but EVERYTHING. So I simply held on to a reference of the existing scheduled notifications before blasting them, and then rescheduled them accordingly:

    UIApplication *app = [UIApplication sharedApplication];
    
    NSLog(@"\nScheduled notif count (prior) = %d", app.scheduledLocalNotifications.count);
    
    NSArray *scheduledNotifs = app.scheduledLocalNotifications; // hold on to a reference
    
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; // blast everything
    
    NSLog(@"\nScheduled notif count (post-wipeout) = %d", app.scheduledLocalNotifications.count);
    
    for (UILocalNotification *notif in scheduledNotifs) {
        [app scheduleLocalNotification:notif]; // put them back
    }
    
    NSLog(@"\nScheduled notif count (post-repopulation) = %d", app.scheduledLocalNotifications.count);
    

    Not sure if that helps anyone but this worked great for my situation.

    0 讨论(0)
  • 2020-12-20 15:15

    After seeing your updated code, it seems you are interested in cancel all the notifications with-in for loop, so you can use -

     [[UIApplication sharedApplication] cancelAllLocalNotifications];
    
    0 讨论(0)
  • 2020-12-20 15:23

    Try this links:

    Apple doc

    Some tutorial

    And local notification is registering to device notification center, not in your app.

    But, if your app is running, and is a notification time, then you can get notification parameters in game in:

    -(void) application:(UIApplication*)app didReceiveLocalNotification:(UILocalNotification*) notification
    {
    // local notification is geted
    }
    
    0 讨论(0)
  • 2020-12-20 15:25

    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.

    0 讨论(0)
  • 2020-12-20 15:32

    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];
        }
    }
    
    0 讨论(0)
提交回复
热议问题