Find list of Local Notification the app has already set

后端 未结 8 1830
盖世英雄少女心
盖世英雄少女心 2020-12-01 05:37

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

相关标签:
8条回答
  • 2020-12-01 05:42

    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)
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 05:44

    In iOS 10, using the new UserNotifications framework:

    UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in
    
            print("Requests: \(notificationRequest)")
    }
    
    0 讨论(0)
  • 2020-12-01 05:54

    UIApplication has a property called scheduledLocalNotifications which returns an optional array containing elements of type UILocalNotification.

    UIApplication.shared.scheduledLocalNotifications
    
    0 讨论(0)
  • 2020-12-01 06:02

    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.

    0 讨论(0)
  • 2020-12-01 06:03

    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

    0 讨论(0)
  • 2020-12-01 06:05

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

    print(UIApplication.sharedApplication().scheduledLocalNotifications)
    
    0 讨论(0)
提交回复
热议问题