Je veux exécuter une UILocalNotification
à 9h00 tous les jours, pour toujours, tant que l'application est ouverte. La chose la plus proche que j'ai trouvée est la suivante:
UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.fireDate = [[NSDate date] dateByAddingTimeInterval:60*60*24]; notification.alertBody = @"It's been 24 hours."; [[UIApplication sharedApplication] scheduleLocalNotification:notification];
Cependant, ce code n'exécute une UILocalNotification
qu'une UILocalNotification
fois en 24 heures, et non à une heure spécifiée. J'ai cherché à utiliser NSDate
manière NSDate
autre, mais je n'ai obtenu aucun résultat.
Le code sera exécuté dans AppDelegate
dans la méthode de l' application didFinishLaunchingWithOptions
. Si quelqu'un devait ouvrir l'application et la placer en arrière-plan à 8h59 , UILocalNotification
s'exécuterait toujours à 9h00 .
Un NSDateComponent
ne fonctionnera pas avec cela car je devrais déclarer une année, un mois et un jour, mais je veux exécuter cette UILocalNotification
tous les jours sans avoir à modifier le code.
Vous devez find l'heure SUIVANTE à 9h00 et définir la notification locale à triggersr à ce moment-là:
NSDate *now = [NSDate date]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now]; [components setHour:9]; // Gives us today's date but at 9am NSDate *next9am = [calendar dateFromComponents:components]; if ([next9am timeIntervalSinceNow] < 0) { // If today's 9am already occurred, add 24hours to get to tomorrow's next9am = [next9am dateByAddingTimeInterval:60*60*24]; } UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.fireDate = next9am; notification.alertBody = @"It's been 24 hours."; // Set a repeat interval to daily notification.repeatInterval = NSDayCalendarUnit; [[UIApplication sharedApplication] scheduleLocalNotification:notification];
Votre question a deux problèmes:
Voici un extrait:
// 1st: find next fire date, using NSDateComponents NSDate * date = [NSDate date]; NSDateComponents * components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:date]; // Components will now contain information about current hour and minute, day, month and year. // Do your calculation in order to setup the right date. Note that components reflect user timezone. // For example, skip to the next day if current time is after 9:00: if (components.hour >= 9) { components.day += 1; } // Now fix the components for firing time, for example 9:00. components.hour = 9; components.minute = 0; NSDate * fireDate = [[NSCalendar currentCalendar] dateFromComponents:components]; NSLog(@"Notification will fire at: %@", fireDate); // 2nd: Schedule local notification with repetitions: UILocalNotification * notification = [[UILocalNotification alloc] init]; notification.fireDate = fireDate; notification.repeatInterval = NSDayCalendarUnit; // Here is the sortingck notification.alertBody = @"It's been 24 hours."; [[UIApplication sharedApplication] scheduleLocalNotification:notification];
repeatInterval
simplement repeatInterval
avant de planifier la notification
localNotification.repeatInterval = NSCalendarUnitDay;