How to create multiple Local Notifications

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:

  • How could I add a shadow to a grouped UITableView (as seen in the official twitter app)?
  • How to convert a UIColor to UInt32 ? I get the error Ambiguous use of operator “+=”
  • Is it possible to create an app iOS 4 - iOS 7 compatible with Xcode 5.0.2 on Mavericks?
  • Core Bluetooth and backgrounding: Detection of a device and triggering an action, even after being days in background mode?
  • Is the opacity and alpha the same thing for UIView
  • Objective C Adding a Contact to a specific Group in the iPhone
  • -(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?

    2 Solutions Collect From Internet About “How to create multiple Local Notifications”

    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…