2013-06-18 26 views
0

我正在製作一個使用本地通知的應用程序。我正在嘗試它,並使用兩個實例。當我運行殺應用程序時,只顯示一個通知。這是在應用程序delegate.m代碼兩個本地通知錯誤

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


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

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

[datecomponent2 setYear:2013]; 
[datecomponent2 setMonth:6]; 
[datecomponent2 setDay:18]; 
[datecomponent2 setHour:16]; 
[datecomponent2 setMinute:58]; 

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


UILocalNotification *notification2 = [[UILocalNotification alloc]init]; 
[notification2 setAlertBody:@"two"]; 
[notification2 setFireDate:dd2.date]; 
[notification2 setTimeZone:[NSTimeZone defaultTimeZone]]; 
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification2]]; 


//2 

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

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

[datecomponent1 setYear:2013]; 
[datecomponent1 setMonth:6]; 
[datecomponent1 setDay:18]; 
[datecomponent1 setHour:16]; 
[datecomponent1 setMinute:59]; 

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


UILocalNotification *notification1 = [[UILocalNotification alloc]init]; 
[notification1 setAlertBody:@"one"]; 
[notification1 setFireDate:dd1.date]; 
[notification1 setTimeZone:[NSTimeZone defaultTimeZone]]; 
[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification1]]; 

}

回答

2

首先,你這樣做:

[application setScheduledLocalNotifications:[NSArray arrayWithObject:notification2]]; 

然後

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

這意味着:取代所有先前預定本地通知(在本例中爲通知2),數組中包含notification1

取而代之的是,你可以使用:

[application scheduleLocalNotification:notification1]; 
[application scheduleLocalNotification:notification2]; 

或:

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