2

我正在尝试在我的应用程序中创建多个本地通知,但由于某种原因,只有第一个通知弹出窗口出现,其余的只是不起作用,这是我的代码。

我有一个名为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];

我做错了什么?

4

2 回答 2

6

问题是您正在覆盖当前通过调用您的-setarNotificacao:nome:函数安排的所有本地通知。这条线

    myapp.scheduledLocalNotifications = arrayOfNOtifications;

将所有当前安排的通知设置为arrayOfNotifications;如果当前安排的通知不在该数组中,则将其取消。

修复方法是使用该-[UIApplication scheduleLocalNotification:]方法来安排通知,它添加给定的通知而不取消任何已安排的通知:

[myapp scheduleLocalNotification:notification];
于 2013-10-31T04:56:39.380 回答
5

试试这个:

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

希望它会有所帮助。

快乐的编码...

于 2013-10-31T04:12:06.593 回答