我想使用UILocalNotification实现推送通知。 所以我写了这段代码,但它多次调用,时间不是23:00而是23:13,23:34,23:xx
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *now = [NSDate date];
NSDateComponents *componentsForFireDate = [calendar components:(NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit | NSWeekdayCalendarUnit) fromDate: now];
[componentsForFireDate setHour: 23] ; //for fixing 23PM hour
[componentsForFireDate setMinute:0] ;
[componentsForFireDate setSecond:0] ;
UILocalNotification* notifyAlarm = [[UILocalNotification alloc]
init];
NSLog(@"____date is %@",[calendar dateFromComponents:componentsForFireDate]);
notifyAlarm.fireDate = [calendar dateFromComponents:componentsForFireDate];
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval = 0;
notifyAlarm.alertBody = @"hello";
[[UIApplication sharedApplication] scheduleLocalNotification:notifyAlarm];
我想修复23:00并拨打一次电话。 你有什么主意吗?
提前致谢。