私のアプリでは、将来、多くのリマインダーを設定できます。アプリケーションが起動したら、どのリマインダ(通知)が既に設定されているかを知りたい。アプリが既に設定しているローカル通知のリストを見つけよう
私が設定した通知やアプリケーション(コアデータやPlistなど)に保存する必要がある通知を読み取ることはできますか?
私のアプリでは、将来、多くのリマインダーを設定できます。アプリケーションが起動したら、どのリマインダ(通知)が既に設定されているかを知りたい。アプリが既に設定しているローカル通知のリストを見つけよう
私が設定した通知やアプリケーション(コアデータやPlistなど)に保存する必要がある通知を読み取ることはできますか?
UIApplication
には、使用できるscheduledLocalNotifications
というプロパティがあります。
こんにちはスコットBerrevoests、私を助けてください。私は2回目から私の警報を得ていないが、保留中の警報リストにすでに入っている。 https://stackoverflow.com/questions/44132879/ios-local-notification-not-firing-second-time-but-shows-in-getpendingnotificatio –
スコットは正しいです。
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
ありがとうございましたこれはまた、各通知のIDを格納に関する私の次の質問に役立ちます - すべて完了しました... –
これは奇妙なサンプルコードです。 OPはローカル通知を表示するよう求めており、ローカル通知をキャンセルする方法の例を示しています – Houman
@Scott Berrevoetsが正解を出しました。
[[[UIApplication sharedApplication] scheduledLocalNotifications] enumerateObjectsUsingBlock:^(UILocalNotification *notification, NSUInteger idx, BOOL *stop) {
NSLog(@"Notification %lu: %@",(unsigned long)idx, notification);
}];
をスウィフトでは、コンソールに印刷されたすべてのあなたの現在スケジュールローカル通知を参照してください:実際にそれらを一覧表示するには、それは、配列内のオブジェクトを列挙するのは簡単です
スウィフト3.0の場合print(UIApplication.sharedApplication().scheduledLocalNotifications)
とスウィフト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.0.2:iOS 10
で
UIApplication.shared.scheduledLocalNotifications
、新しいUserNotifications
フレームワークの使用:
UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in
print("Requests: \(notificationRequest)")
}
スイフト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.
}
}
})
を私は同じバグを修正するためにこれを使用毎週の通知が既に設定されていて、アプリが開くと再び設定されるため、タイマーをリセットしたままにしておくとそれは決して出現しませんでしたか?
今後の通知を見つける方法はありますか? (繰り返しの間隔を持つものを含む) –