UILocalNotification Schehduling问题

时间:2014-12-12 10:38:19

标签: ios objective-c uilocalnotification

我已安排本地通知在下午4点开火,并于每天下午4点发出通知,但通知会在下午3点44分,下午3点47分之前发出。

这是代码 -

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit ) fromDate:self.datePicker.date];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:self.datePicker.date];
[dateComponents setHour:[timeComponents hour]];
[dateComponents setMinute:[timeComponents minute]];
[dateComponents setSecond:0.0];

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = [NSString stringWithFormat:@"How are you feeling?"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.fireDate = [calendar dateFromComponents:dateComponents];
localNotification.repeatInterval = kCFCalendarUnitDay;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

1 个答案:

答案 0 :(得分:0)

试试这个:

NSDate *myDate = [NSDate date]; // Set your time here
myDate = [myDate dateByAddingTimeInterval:1*24*60*60];
UILocalNotification  *notif = [[UILocalNotification alloc] init];
notif.fireDate = myDate;
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = [NSString stringWithFormat:@"How are you feeling?"];
notif.alertBody = @"Reminder is set";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];

希望它会对你有所帮助。