私のアプリでは、ユーザーが将来のいくつかのリマインダーを設定できます。アプリの起動時に、すでに設定されているリマインダー (通知) を知りたい。
設定した通知を読み返すことはできますか、それともアプリに保存する必要がありますか (Core Data や Plist など)?
私のアプリでは、ユーザーが将来のいくつかのリマインダーを設定できます。アプリの起動時に、すでに設定されているリマインダー (通知) を知りたい。
設定した通知を読み返すことはできますか、それともアプリに保存する必要がありますか (Core Data や Plist など)?
UIApplication
scheduledLocalNotifications
タイプ の要素を含むオプションの配列を返すというプロパティがありますUILocalNotification
。
UIApplication.shared.scheduledLocalNotifications
Swift 3.0 および Swift 4.0 の場合
することを忘れないでくださいimport UserNotifications
クラスUNUserNotificationCenterは古いバージョンでは利用できないため、これはiOS10以降およびwatchOS3以降で機能します(リンク)
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)
}
}
}
スコットは正しいです。
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
@Scott Berrevoets が正しい答えを出しました。それらを実際にリストするには、配列内のオブジェクトを列挙するのは簡単です。
[[[UIApplication sharedApplication] scheduledLocalNotifications] enumerateObjectsUsingBlock:^(UILocalNotification *notification, NSUInteger idx, BOOL *stop) {
NSLog(@"Notification %lu: %@",(unsigned long)idx, notification);
}];
Swift で、現在スケジュールされているすべてのローカル通知をコンソールに表示するには:
print(UIApplication.sharedApplication().scheduledLocalNotifications)