Jusqu'à iOS 9
nous écrivons local notifications
comme celle-ci
UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = pickerDate; localNotification.alertBody = self.textField.text; localNotification.timeZone = [NSTimeZone defaultTimeZone]; localNotification.repeatInterval = NSCalendarUnitMinute; localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
et dans la notification locale, nous avons repeatInterval
, maintenant dans WWDC2016
Apple a annoncé la User Notification
qui contient
UNCalendarNotificationTrigger.
UNTimeIntervalNotificationTrigger* sortinggger = [UNTimeIntervalNotificationTrigger sortingggerWithTimeInterval:60 repeats:YES];
le code ci-dessus triggersra une notification après chaque minute. Mais ne peut pas définir la date.
NSDateComponents* date = [[NSDateComponents alloc] init]; date.hour = 8; date.minute = 30; UNCalendarNotificationTrigger* sortingggerC = [UNCalendarNotificationTrigger sortingggerWithDateMatchingComponents:date repeats:YES];
le code au-dessus de la date peut être réglé et la répétition se triggersra demain à 8h30 et non pas après la minute.
Dans iOS 10 User Notification
comment je peux définir l'heure de la date avec la fréquence de répétition comme nous pouvons définir dans UILocalNotification
?
Je veux planifier la User Notification
demain à 20h30 et continue à répéter après chaque minute, tout comme le code que j'ai spécifié en haut de local notification
Inspiré par @Ramon Vasconcelos, j'utilise le code suivant pour configurer les intervalles et fireDate set
switch (interval) { case NSCalendarUnitMinute: { unitFlags = NSCalendarUnitSecond; break; } case NSCalendarUnitHour: { unitFlags = NSCalendarUnitMinute | NSCalendarUnitSecond; break; } case NSCalendarUnitDay: { unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; break; } case NSCalendarUnitWeekOfYear: { unitFlags = NSCalendarUnitWeekday | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; break; } case NSCalendarUnitMonth:{ unitFlags = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; } case NSCalendarUnitYear:{ unitFlags = NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; } default: unitFlags = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond; break; } NSDateComponents *components = [[NSCalendar currentCalendar] components:unitFlags fromDate:fireDate]; UNCalendarNotificationTrigger *sortinggger = [UNCalendarNotificationTrigger sortingggerWithDateMatchingComponents:components repeats:interval != 0];
Pour iOS 10, vous pouvez utiliser comme ceci:
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:fireDate]; UNCalendarNotificationTrigger* sortinggger = [UNCalendarNotificationTrigger sortingggerWithDateMatchingComponents:components repeats:YES];
il a le même effet à partir du code iOS 9. Pour répéter, il suffit d'utiliser les composants que vous voulez répéter.