문제

I'm trying to create multiple local notifications, in my app, but for some reason only the First Notification Pop's up, the rest just does not work, this is my code.

I have a class named criaAlertas, which is responsible for creating the notifications, in that class i have the following method:

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

So i instantiated a object of that class, and tried to do this:

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

What i'm doing wrong?

도움이 되었습니까?

해결책

The problem is that you're overwriting all of the local notifications currently scheduled with the call to your -setarNotificacao:nome: function. This line

    myapp.scheduledLocalNotifications = arrayOfNOtifications;

sets all of the currently scheduled notifications to arrayOfNotifications; if a notification currently scheduled is not in that array, then it is canceled.

The fix is to use the -[UIApplication scheduleLocalNotification:] method to schedule the notification, which adds the given notification without canceling any notifications already scheduled:

[myapp scheduleLocalNotification:notification];

다른 팁

Try This one:

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

Hope it will help.

Happy Coding...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top