2013-07-08 47 views
24

我的應用允許用戶在將來設置多個提醒。當應用程序啓動時,我想知道已經設置了什麼提醒(通知)。查找該應用已經設置的本地通知列表

我可以讀回已設置的通知或我需要在我的應用程序中存儲的通知(例如Core Data或Plist)嗎?

+0

有沒有辦法找到即將到來的通知? (包括有重複間隔的那個) –

回答

39

UIApplication有一個名爲scheduledLocalNotifications的屬性,您可以使用它。

+0

嘿斯科特Berrevoests,請幫我。我從第二次沒有收到警報,但已經在待處理的警報列表中。 https://stackoverflow.com/questions/44132879/ios-local-notification-not-firing-second-time-but-shows-in-getpendingnotificatio –

21

斯科特是正確的。

UIApplication的財產scheduledLocalNotifications

下面的代碼:

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 

    } 

} 

欲瞭解更多信息,請查看:scheduledLocalNotifications example UIApplication ios

+0

感謝這也有助於我的下一個問題有關存儲每個通知的ID - 現在全部完成... –

+1

這是一個奇怪的示例代碼。 OP要求列出本地通知,並且您正在展示如何取消本地通知的示例 – Houman

5

@Scott Berrevoets給出了正確的答案。要真正一一列舉了,很簡單枚舉對象數組中:

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

在斯威夫特,看到打印到控制檯上所有當前安排本地通知:

print(UIApplication.sharedApplication().scheduledLocalNotifications) 
12

對於雨燕3.0雨燕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) 
     } 
    } 
} 
3

夫特3.0.2:

UIApplication.shared.scheduledLocalNotifications 
1

iOS 10,使用新UserNotifications框架:

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

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

夫特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. 

     } 
    } 
}) 

我用這個修正錯誤,其中相同的每週通知已經設置,並在應用程序打開時再次設置,所以它會保持重置計時器以顯示,這是我它從來沒有出現過。

相關問題