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

时间:2021-12-16 23:10:42

Hi in my Application I have Multiple events for every event i need to create one UILocalNotification for example like I want to say wishes on Fathers day,Mothers and some festivals. So I already i have the specific date now I want to fire it on the particular day please how to do this. I have already done for single UILocalNotification Now I need for multiple firedates.

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

My code.

我的代码。

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

  }

I have used this above code for single firedate UILocalNotification please tell me how to do it for multiple firedates.

我已经使用上面的代码进行单一的激活UILocalNotification,请告诉我如何为多个激发者做这件事。

Thanks.

谢谢。

2 个解决方案

#1


2  

Here is the answer, In compsForFireDate you can ad multiple date and you can call it yearly

这是答案,在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];

    }

#2


1  

I'm guessing you are registering the notification with setScheduledLocalNotifications: which will replace any existing notification. This is clearly stated in the documentation.

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

To schedule a single UILocalNotification use -(void)scheduleLocalNotification:(UILocalNotification *)notification

要安排单个UILocalNotification,请使用 - (void)scheduleLocalNotification:(UILocalNotification *)通知

Here I have create a methods that take a date and a string to schedule an UILocalNotification:

在这里,我创建了一个方法,它采用日期和字符串来安排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];
}

#1


2  

Here is the answer, In compsForFireDate you can ad multiple date and you can call it yearly

这是答案,在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];

    }

#2


1  

I'm guessing you are registering the notification with setScheduledLocalNotifications: which will replace any existing notification. This is clearly stated in the documentation.

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

To schedule a single UILocalNotification use -(void)scheduleLocalNotification:(UILocalNotification *)notification

要安排单个UILocalNotification,请使用 - (void)scheduleLocalNotification:(UILocalNotification *)通知

Here I have create a methods that take a date and a string to schedule an UILocalNotification:

在这里,我创建了一个方法,它采用日期和字符串来安排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];
}