2013-07-08 44 views
24

Ứng dụng của tôi cho phép người dùng đặt một số lời nhắc trong tương lai. Khi các ứng dụng lauches tôi muốn biết những gì nhắc nhở (thông báo) đã được thiết lập.Tìm danh sách Thông báo địa phương mà ứng dụng đã đặt

Tôi có thể đọc lại các thông báo tôi đã đặt hoặc tôi cần lưu trữ trong ứng dụng của mình (ví dụ: Dữ liệu cốt lõi hoặc Plist) không?

+0

Có cách nào để tìm thông báo sắp tới không? (bao gồm một trong đó có khoảng thời gian lặp lại) –

Trả lời

39

UIApplication có thuộc tính được gọi là scheduledLocalNotifications mà bạn có thể sử dụng.

+0

Hey scott Berrevoests, Xin hãy giúp tôi ra ngoài. Tôi không nhận được báo thức của mình từ lần thứ hai nhưng đã có trong danh sách báo thức đang chờ xử lý. https://stackoverflow.com/questions/44132879/ios-local-notification-not-firing-second-time-but-shows-in-getpendingnotificatio –

21

Scott là chính xác.

UIApplication 's tài sản scheduledLocalNotifications

Dưới đây là các mã:

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 

    } 

} 

Để biết thêm thông tin, hãy xem điều này: scheduledLocalNotifications example UIApplication ios

+0

Cảm ơn điều này cũng giúp với câu hỏi tiếp theo của tôi về việc lưu trữ ID cho mỗi thông báo - tất cả được thực hiện ngay bây giờ ... –

+1

Đây là một mã mẫu lạ. OP yêu cầu liệt kê các thông báo địa phương và bạn đang hiển thị ví dụ về cách hủy thông báo địa phương – Houman

5

@Scott Berrevoets đã đưa ra câu trả lời đúng. Để thực sự liệt kê chúng, nó là đơn giản để liệt kê các đối tượng trong mảng:

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

Trong Swift, để xem tất cả các thông báo địa phương hiện đang lên kế hoạch của bạn được in trong bảng điều khiển

print(UIApplication.sharedApplication().scheduledLocalNotifications) 
12

Đối Swift 3.0 và Swift 4,0

đừng quên để làm 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) 
     } 
    } 
} 
.210
3

Swift 3.0.2:

UIApplication.shared.scheduledLocalNotifications 
1

Trong iOS 10, sử dụng UserNotifications khuôn khổ mới:

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

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

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. 

     } 
    } 
}) 

Tôi sử dụng này để sửa chữa một lỗi nơi cùng thông báo hàng tuần đã được đặt và được đặt lại khi ứng dụng mở, do đó, nó sẽ tiếp tục đặt lại bộ hẹn giờ xuất hiện, mà tôi ans nó không bao giờ xuất hiện.