Find list of Local Notification the app has already set

扶醉桌前 提交于 2019-12-17 09:22:28

问题


My app allows users to set a number of reminders in the future. When the app lauches I want to know what reminders (notifications) have already been set.

Can I read back the notifications I have set or do I need to store in my app (e.g. Core Data or Plist)?


回答1:


UIApplication has a property called scheduledLocalNotifications which you can use.




回答2:


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)
        }
    }
}



回答3:


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




回答4:


@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);
}];



回答5:


Swift 3.0.2:

UIApplication.shared.scheduledLocalNotifications



回答6:


In iOS 10, using the new UserNotifications framework:

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

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



回答7:


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.




回答8:


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

print(UIApplication.sharedApplication().scheduledLocalNotifications)


来源:https://stackoverflow.com/questions/17531332/find-list-of-local-notification-the-app-has-already-set

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