40

私のアプリでは、ユーザーが将来のいくつかのリマインダーを設定できます。アプリの起動時に、すでに設定されているリマインダー (通知) を知りたい。

設定した通知を読み返すことはできますか、それともアプリに保存する必要がありますか (Core Data や Plist など)?

4

8 に答える 8

43

UIApplicationscheduledLocalNotificationsタイプ の要素を含むオプションの配列を返すというプロパティがありますUILocalNotification

UIApplication.shared.scheduledLocalNotifications
于 2013-07-08T16:18:27.070 に答える
34

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)
        }
    }
}
于 2016-08-19T08:35:41.210 に答える
22

スコットは正しいです。

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

于 2013-07-08T16:32:41.437 に答える
6

@Scott Berrevoets が正しい答えを出しました。それらを実際にリストするには、配列内のオブジェクトを列挙するのは簡単です。

[[[UIApplication sharedApplication] scheduledLocalNotifications] enumerateObjectsUsingBlock:^(UILocalNotification *notification, NSUInteger idx, BOOL *stop) {
    NSLog(@"Notification %lu: %@",(unsigned long)idx, notification);
}];
于 2014-06-29T02:03:30.327 に答える
1

Swift で、現在スケジュールされているすべてのローカル通知をコンソールに表示するには:

print(UIApplication.sharedApplication().scheduledLocalNotifications)
于 2016-02-27T19:45:44.333 に答える