如何为多个被激活者创建UILocalNotification

时间:2014-07-08 10:24:33

标签: ios uilocalnotification

嗨,在我的应用程序中,我需要创建一个UILocalNotification的每个事件的多个事件,例如我想在父亲节,母亲和一些节日上表达祝福。所以我已经有了特定的日期,我想在特定的日子解雇它,请该怎么做。我已经为单UILocalNotification做了我现在需要多个firedates

我的代码。

    - (void)applicationDidEnterBackground:(UIApplication *)application
  {

       NSCalendar *regCalender =[NSCalendar currentCalendar];
       NSDateComponents *dateComponent = [regCalender components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];

      [dateComponent setYear:2014];
      [dateComponent setMonth:7];
      [dateComponent setDay:8];
      [dateComponent setHour:15];
      [dateComponent setMinute:31];

      UIDatePicker *dd = [[UIDatePicker alloc]init];
     [dd setDate:[regCalender dateFromComponents:dateComponent]];
     UILocalNotification *notification = [[UILocalNotification alloc]init];
     [notification setAlertBody:@"Welcome"];
     [notification setFireDate:dd.date];
     [notification setTimeZone:[NSTimeZone defaultTimeZone]];
      notification.soundName=@"double_tone.mp3";
     [application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];

  }

我已经将上面的代码用于单个被激活的UILocalNotification请告诉我如何为多个被激活的人做。

感谢。

2 个答案:

答案 0 :(得分:2)

这是答案, 在compsForFireDate中,您可以发布多个日期,您可以每年调用它

 -(void) YearlyNotification: (int)year :(UILocalNotification *)notification : (NSDate*) alramDate
    {
        NSCalendar *calendar = [NSCalendar currentCalendar];

        NSDateComponents *compsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: alramDate];
        [compsForFireDate setHour: 10] ; //for fixing 10AM hour
        [compsForFireDate setMinute:0] ;
        [compsForFireDate setSecond:0] ;
        notification.repeatInterval = NSYearCalendarUnit;
        notification.fireDate=[calendar dateFromComponents:compsForFireDate];
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];

    }

答案 1 :(得分:1)

我猜你正在使用setScheduledLocalNotifications:注册通知,这将取代任何现有通知。这在文档中有明确说明。

安排单UILocalNotification次使用-(void)scheduleLocalNotification:(UILocalNotification *)notification

在这里,我创建了一个方法,它采用日期和字符串来安排UILocalNotification

-(void) addLocalNotificationForDate:(NSDate *)date withAlertBody:(NSString *)alertBody {

    UILocalNotification *notification = [[UILocalNotification alloc]init];
    [notification setAlertBody:alertBody];
    [notification setFireDate:date];
    [notification setTimeZone:[NSTimeZone defaultTimeZone]];
    notification.soundName=@"double_tone.mp3";

    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}