Comment créer plusieurs notifications locales

J'essaie de créer plusieurs notifications locales, dans mon application, mais pour une raison quelconque, le First Notification Pop ne fonctionne pas, le rest ne fonctionne pas, c'est mon code.

J'ai une class nommée criaAlertas , qui est responsable de la création des notifications, dans cette class j'ai la méthode suivante:

-(void)setarNotificacao:(NSInteger)quando nome:(UILocalNotification *)notification { UIApplication *myapp = [UIApplication sharedApplication]; notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:quando]; notification.alertBody = @"Nice"; notification.timeZone = [NSTimeZone defaultTimeZone]; notification.soundName = UILocalNotificationDefaultSoundName; NSMutableArray *arrayOfNOtifications = [[NSMutableArray alloc]init]; [arrayOfNOtifications addObject:notification]; myapp.scheduledLocalNotifications = arrayOfNOtifications; } 

J'ai donc instancié un object de cette class, et j'ai essayé de faire ceci:

  criaAlertas *novoAlerta = [[criaAlertas alloc]init]; UILocalNotification *notaUm = [[UILocalNotification alloc]init]; UILocalNotification *notaDois = [[UILocalNotification alloc]init]; [novoAlerta setarNotificacao:15 nome:notaUm]; [novoAlerta setarNotificacao:30 nome:notaDois]; 

Qu'est-ce que je fais mal?

Le problème est que vous écrasez toutes les notifications locales actuellement planifiées avec l'appel de votre fonction -setarNotificacao:nome: Cette ligne

  myapp.scheduledLocalNotifications = arrayOfNOtifications; 

définit toutes les notifications actuellement planifiées sur arrayOfNotifications ; Si une notification actuellement planifiée ne figure pas dans ce tableau, elle est annulée.

Le correctif consiste à utiliser la méthode -[UIApplication scheduleLocalNotification:] pour planifier la notification, qui ajoute la notification donnée sans annuler les notifications déjà planifiées:

 [myapp scheduleLocalNotification:notification]; 

Essaye celui-là:

  -(IBAction)setRemind:(id)sender { NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init]; dateFormatter2 setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; //Gets our picker NSDate *selectedTime = [datePicker date]; strDate2 = [dateFormatter2 ssortingngFromDate:selectedTime]; NSDate *Date=[dateFormatter2 dateFromSsortingng:strDate2]; NSLog(@"selected Date fro str Over Here =======>>>>>>>>>>>>%@",Date); NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:Date]; // Set up the fire time NSDateComponents *dateComp = [[NSDateComponents alloc] init]; [dateComp setDay:[dateComponents day]]; [dateComp setMonth:[dateComponents month]]; [dateComp setYear:[dateComponents year]]; [dateComp release]; NSDate *date = [calendar dateFromComponents:dateComp]; [self scheduleAlarmForDate:date message:@"My First Local Notification."]; } - (IBAction)scheduleAlarmForDate:(NSDate*)date message:(NSSsortingng*)msg { //====== TO SEE OLD NOTIFI======= UIApplication *Ap = [UIApplication sharedApplication]; NSArray *arr = [Ap scheduledLocalNotifications]; NSLog(@"Old Notifications :>> %@",arr); UIApplication* app = [UIApplication sharedApplication]; UILocalNotification *alarm = [[UILocalNotification alloc] init]; // Create a new notification alarm.fireDate = date; NSLog(@"fireDate IS >> %@", alarm.fireDate); alarm.timeZone = [NSTimeZone localTimeZone]; alarm.alertBody = msg; NSLog(@"msg IS >> %@",msg); alarm.alertAction = @"Show"; alarm.repeatInterval = NSDayCalendarUnit * 24 * 60 // it repeat every day ,set it as per you want alarm.soundName = UILocalNotificationDefaultSoundName; alarm.applicationIconBadgeNumber = 1; [app scheduleLocalNotification:alarm]; [alarm release]; } 

J'espère que ça va aider.

Bonne codification …