查找应用已设置的本地通知列表

Rec*_*eel 33 ios uilocalnotification

我的应用程序允许用户将来设置一些提醒.当应用程序启动时,我想知道已经设置了什么提醒(通知).

我可以回读我已设置的通知或我需要存储在我的应用程序中的通知(例如核心数据或Plist)吗?

Sco*_*ets 42

UIApplication有一个scheduledLocalNotifications你可以使用的属性.

  • 请注意,不建议使用“ scheduledLocalNotifications” (2认同)

Fri*_*zzo 25

适用于Swift 3.0和Swift 4.0

别忘了做 import UserNotifications

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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Com*_*sky 21

斯科特是对的.

UIApplication的属性预定本地通知

这是代码:

NSMutableArray *notifications = [[NSMutableArray alloc] init];
[notifications addObject:notification];
app.scheduledLocalNotifications = notifications;
//Equivalent: [app setScheduledLocalNotifications:notifications];
Run Code Online (Sandbox Code Playgroud)
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;
    }
}
Run Code Online (Sandbox Code Playgroud)
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

    }

}
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请查看:scheduledLocalNotifications示例UIApplication ios


sib*_*urb 6

@Scott Berrevoets给出了正确答案.要实际列出它们,枚举数组中的对象很简单:

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