Dismiss an already delivered UILocalNotification?

后端 未结 6 1437
无人及你
无人及你 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: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];
        }
    }
    

提交回复
热议问题