Как да създадете множество локални известия

Опитвам се да създам множество локални известия в моето приложение, но по някаква причина се появява само първото изскачащо известие, останалите просто не работят, това е моят код.

Имам клас с име criaAlertas, който отговаря за създаването на известията, в този клас имам следния метод:

-(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;
}

Така че аз инстанцирах обект от този клас и се опитах да направя това:

    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];

Какво правя грешно?


person André Oliveira    schedule 31.10.2013    source източник


Отговори (2)


Проблемът е, че презаписвате всички локални известия, планирани в момента с извикването на вашата функция -setarNotificacao:nome:. Тази линия

    myapp.scheduledLocalNotifications = arrayOfNOtifications;

задава всички текущо планирани известия на arrayOfNotifications; ако текущо планирано известие не е в този масив, то се отменя.

Решението е да използвате -[UIApplication scheduleLocalNotification:] метод за планиране на известието, който добавя даденото известие, без да анулира всички вече планирани известия:

[myapp scheduleLocalNotification:notification];
person Adam Rosenfield    schedule 31.10.2013
comment
Благодаря, точно това беше проблема! - person André Oliveira; 31.10.2013
comment
@Adam Rosenfield, можете ли да добавите пълния код за създаване и изтриване на множество известия? - person Vasanth; 21.01.2019

Опитай това:

  -(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 stringFromDate:selectedTime];


      NSDate *Date=[dateFormatter2 dateFromString: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:(NSString*)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];
  } 

Надявам се, че ще помогне.

Приятно кодиране...

person Dhaval Bhadania    schedule 31.10.2013
comment
какво ще кажете за iOS 10? ако искам да задействам локално известие за повече от 1 известие? - person Anil Gupta; 28.12.2016
comment
какво точно искаш?? @AnilGupta - person Dhaval Bhadania; 28.12.2016
comment
Искам да добавя локално известие в iOS 10 и искам да задам 3 бъдещи дати за моето персонализирано напомняне за събитие. - person Anil Gupta; 28.12.2016
comment
можете да зададете така ... може да ви помогне по-добре: appcoda.com/ios10 -user-notifications-guide - person Dhaval Bhadania; 28.12.2016