2013-07-08 7 views
24

私のアプリでは、将来、多くのリマインダーを設定できます。アプリケーションが起動したら、どのリマインダ(通知)が既に設定されているかを知りたい。アプリが既に設定しているローカル通知のリストを見つけよう

私が設定した通知やアプリケーション(コアデータやPlistなど)に保存する必要がある通知を読み取ることはできますか?

+0

今後の通知を見つける方法はありますか? (繰り返しの間隔を持つものを含む) –

答えて

39

UIApplicationには、使用できるscheduledLocalNotificationsというプロパティがあります。

+0

こんにちはスコットBerrevoests、私を助けてください。私は2回目から私の警報を得ていないが、保留中の警報リストにすでに入っている。 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

をスウィフトでは、コンソールに印刷されたすべてのあなたの現在スケジュールローカル通知を参照してください:実際にそれらを一覧表示するには、それは、配列内のオブジェクトを列挙するのは簡単です

スウィフト3.0の場合
print(UIApplication.sharedApplication().scheduledLocalNotifications) 
12

とスウィフト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:iOS 10

UIApplication.shared.scheduledLocalNotifications 
1

、新しい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. 

     } 
    } 
}) 

を私は同じバグを修正するためにこれを使用毎週の通知が既に設定されていて、アプリが開くと再び設定されるため、タイマーをリセットしたままにしておくとそれは決して出現しませんでしたか?

関連する問題