Dismiss an already delivered UILocalNotification?

后端 未结 6 1438
无人及你
无人及你 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: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.

提交回复
热议问题