How to create multiple Local Notifications

后端 未结 2 574
北荒
北荒 2021-01-15 15:58

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.

相关标签:
2条回答
  • 2021-01-15 16:25

    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...

    0 讨论(0)
  • 2021-01-15 16:30

    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];
    
    0 讨论(0)
提交回复
热议问题