2

我正在寻找安排不同时间和日期的多个 UILocalNotification。我在这里找到了一些答案,我做了这个:

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

//1
NSCalendar *gregcalendar1 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *datecomponent1 = [gregcalendar1 components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];

[datecomponent1 setYear:2013];
[datecomponent1 setMonth:7];
[datecomponent1 setDay:21];
[datecomponent1 setHour:10];
[datecomponent1 setMinute:31];

UIDatePicker *dd1 = [[UIDatePicker alloc]init];
[dd1 setDate:[gregcalendar1 dateFromComponents:datecomponent1]];


UILocalNotification *notification1 = [[UILocalNotification alloc]init];
[notification1 setAlertBody:@"01"];
[notification1 setFireDate:dd1.date];
[notification1 setTimeZone:[NSTimeZone defaultTimeZone]];



//2
NSCalendar *gregcalendar2 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *datecomponent2 = [gregcalendar2 components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];

[datecomponent2 setYear:2013];
[datecomponent2 setMonth:7];
[datecomponent2 setDay:21];
[datecomponent2 setHour:10];
[datecomponent2 setMinute:32];

UIDatePicker *dd2 = [[UIDatePicker alloc]init];
[dd2 setDate:[gregcalendar2 dateFromComponents:datecomponent2]];


UILocalNotification *notification2 = [[UILocalNotification alloc]init];
[notification2 setAlertBody:@"02"];
[notification2 setFireDate:dd2.date];
[notification2 setTimeZone:[NSTimeZone defaultTimeZone]];



//3
NSCalendar *gregcalendar3 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *datecomponent3 = [gregcalendar3 components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];

[datecomponent3 setYear:2013];
[datecomponent3 setMonth:7];
[datecomponent3 setDay:21];
[datecomponent3 setHour:10];
[datecomponent3 setMinute:33];

UIDatePicker *dd3 = [[UIDatePicker alloc]init];
[dd3 setDate:[gregcalendar3 dateFromComponents:datecomponent3]];


UILocalNotification *notification3 = [[UILocalNotification alloc]init];
[notification3 setAlertBody:@"03"];
[notification3 setFireDate:dd3.date];
[notification3 setTimeZone:[NSTimeZone defaultTimeZone]];


[application setScheduledLocalNotifications:@[notification1, notification2, notification3]];}

但是有些通知会随机工作,有些则不起作用。我该怎么办,安排多个通知。

谢谢大家的帮助...

4

0 回答 0