Find list of Local Notification the app has already set

匆匆过客 提交于 2019-11-27 07:25:53

UIApplication has a property called scheduledLocalNotifications which you can use.

Frizzo

For Swift 3.0 and Swift 4.0

don't forget to do import UserNotifications

This is working for iOS10+ and watchOS3+ since the class UNUserNotificationCenter is not available for older versions (link)

let center = UNUserNotificationCenter.current()

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    center.getPendingNotificationRequests { (notifications) in
        print("Count: \(notifications.count)")
        for item in notifications {
          print(item.content)
        }
    }
}

Scott is correct.

UIApplication's property scheduledLocalNotifications

Here's the code:

NSMutableArray *notifications = [[NSMutableArray alloc] init];
[notifications addObject:notification];
app.scheduledLocalNotifications = notifications;
//Equivalent: [app setScheduledLocalNotifications:notifications];

UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++)
{
    UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
    NSDictionary *userInfoCurrent = oneEvent.userInfo;
    NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"uid"]];
    if ([uid isEqualToString:uidtodelete])
    {
        //Cancelling local notification
        [app cancelLocalNotification:oneEvent];
        break;
    }
}

NSArray *arrayOfLocalNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications] ;

for (UILocalNotification *localNotification in arrayOfLocalNotifications) {

    if ([localNotification.alertBody isEqualToString:savedTitle]) {
        NSLog(@"the notification this is canceld is %@", localNotification.alertBody);

        [[UIApplication sharedApplication] cancelLocalNotification:localNotification] ; // delete the notification from the system

    }

}

For more info, check out this: scheduledLocalNotifications example UIApplication ios

@Scott Berrevoets gave the correct answer. To actually list them, it is simple to enumerate the objects in the array:

[[[UIApplication sharedApplication] scheduledLocalNotifications] enumerateObjectsUsingBlock:^(UILocalNotification *notification, NSUInteger idx, BOOL *stop) {
    NSLog(@"Notification %lu: %@",(unsigned long)idx, notification);
}];

Swift 3.0.2:

UIApplication.shared.scheduledLocalNotifications

In iOS 10, using the new UserNotifications framework:

UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in

        print("Requests: \(notificationRequest)")
}

Swift 4

UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: { (requests) in

    for request in requests {

        if request.identifier == "IDENTIFIER YOU'RE CHECKING IF EXISTS" {

            //Notification already exists. Do stuff.

        } else if request === requests.last {

            //All requests have already been checked and notification with identifier wasn't found. Do stuff.

        }
    }
})

I used this to fix a bug where the same weekly notification was already set and being set again when the app would open, so it would keep resetting the timer to appear, which means it never did appear.

In Swift, to see all your currently scheduled local notifications printed in the console:

print(UIApplication.sharedApplication().scheduledLocalNotifications)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!