如何在 iOS SDK 中多次根据 运行 相同的 UILocalNotification 的时间获取下一个日期?

How to take next coming date as per the time to run same NSLocalNotification multiple time in iOS SDK?

我正在开发一个闹钟应用程序,我将从服务器获取时间,并使用逗号分隔值表示 (10:00, 21:00)。通知应在设置的时间每天弹出。

现在我想让应用 运行 多次显示相同的通知。如何从NSDate中取出下一个即将到来的日期和时间,以方便报警系统。假设我在 7:00 打开应用程序,我将不得不 运行 2 条通知(10:00 和 21:00),但如果我在 13:00 打开应用程序,我会必须 运行 只有 1 个通知,即 21:00.

同样在第二天,它应该 运行 两次,即在 (10:00 & 21:00)。我这样做:

- (BOOL)application:(UIApplication *)application  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
     UIUserNotificationSettings *notiSett = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:NULL];
     [[UIApplication sharedApplication] registerUserNotificationSettings: notiSett];
     [self generateLocalNotificationDaily];
}

- (void)generateLocalNotificationDaily
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    NSString *strTime = [Helper getPREF:PREF_AlARM_DAILY_TIME];
    NSArray *arrtime = [strTime componentsSeparatedByString:@":"];
    int hh = (int)[[arrtime objectAtIndex:0]integerValue];
    int mm  = (int)[[arrtime objectAtIndex:1]integerValue];

    NSDate *now = [NSDate date];
    NSTimeInterval secondsPerDay = 24 * 60 * 60;
    NSDate *date = [now dateByAddingTimeInterval:secondsPerDay];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:date];
    [components setHour:hh];
    [components setMinute:mm];

    NSDate *todaySpecificTime = [calendar dateFromComponents:components];

    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = todaySpecificTime;
    localNotification.alertBody = @"Your notification!";
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
}

我可以 运行 一次通知,但不能 运行 多次相同的通知。

您可以将 repeatInterval 设置为您想要的任何重复持续时间。

UILocalNotification *localNot = [[UILocalNotification alloc] init];
localNot.repeatInterval = //Calendar unit for which you need to repeat;

repeatInterval 可以有以下值

    NSCalendarUnitEra                = kCFCalendarUnitEra,
    NSCalendarUnitYear               = kCFCalendarUnitYear,
    NSCalendarUnitMonth              = kCFCalendarUnitMonth,
    NSCalendarUnitDay                = kCFCalendarUnitDay,
    NSCalendarUnitHour               = kCFCalendarUnitHour,
    NSCalendarUnitMinute             = kCFCalendarUnitMinute,
    NSCalendarUnitSecond             = kCFCalendarUnitSecond,
    NSCalendarUnitWeekday            = kCFCalendarUnitWeekday,
    NSCalendarUnitWeekdayOrdinal     = kCFCalendarUnitWeekdayOrdinal,

您不能选择 UILocalnotification 以自定义重复intervals.You只能选择 NSCalendarUnit 间隔中的一个。

此外,无论何时启动应用程序,您都可以获取所有已安排的 UILocalNotifications,并对其进行任何编辑。

        UIApplication *app = [UIApplication sharedApplication];
        for (UILocalNotification *localNotification in [app scheduledLocalNotifications])
        {
//Do whatever you need to do with them (cancel them, tweak times etc.)
        }