在不同的日期激发多个UILocalNotification

时间:2014-07-09 09:09:57

标签: ios oop uilocalnotification

您好,在我的应用程序中,我希望为多个事件显示UILocalNotification。例如母亲节,新年等。所以我已经有了所有活动的日期,我不希望任何活动与用户约会。我需要的是在不同月份的特定日期显示UILocalNotification,请告诉我们可以做什么以及如何做。

我已经厌倦了这样的单UILocaNotification

   - (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:9];
    [dateComponent setHour:16];
    [dateComponent setMinute:0];

    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";

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

    [dateComponent1 setYear:2014];
    [dateComponent1 setMonth:7];
    [dateComponent1 setDay:10];
    [dateComponent1 setHour:16];
    [dateComponent1 setMinute:0];

   UIDatePicker *dd1 = [[UIDatePicker alloc]init];
   [dd setDate:[regCalender1 dateFromComponents:dateComponent]];
   UILocalNotification *notification1 = [[UILocalNotification alloc]init];
   [notification1 setAlertBody:@"home"];
   [notification1 setFireDate:dd1.date];
   [notification1 setTimeZone:[NSTimeZone defaultTimeZone]];
   notification1.soundName=@"double_tone.mp3";


   [application setScheduledLocalNotifications:@[ notification, notification1 ]]; 


   }

我已在我的Appdelegate中使用此代码用于单UILocalNotificaiton,请告诉我如何处理多个代码。我尝试过这样的东西不起作用。

    [application setScheduledLocalNotifications:@[ notification, notification1 ]];  

1 个答案:

答案 0 :(得分:0)

执行此操作时:

[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification1]]; 

您每次都会替换所有通知。

相反,请尝试:

[application setScheduledLocalNotifications:@[ notification, notification1 ]]; 

您可以在其中创建一个包含所有通知的数组,并同时安排它们。

替代方案是多次调用scheduleLocalNotification:,但效率较低。