我想每周晚上 10 点(无论哪个国家/地区)发出通知。我需要使用时区吗?目前,我使用下面的代码每周触发通知,但如何在晚上 10 点准确触发通知。
NSDate *date = [NSDate date];
NSDate* myNewDate = [date dateByAddingTimeInterval:7*24*60*60];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = myNewDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSWeekCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
最佳答案
我认为将 fireDate 设置为您想要收到通知的当天的 22:00(而不是“从现在起一周后”)可以满足您的要求。您可能需要使用 NSCalendar
来执行此操作。
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
NSDate *currentDate = [NSDate date];
NSDate *fireDate = nil;
[dateComponents setDay:3]; // ...or whatever day.
[dateComponents setHour:22];
[dateComponents setMinute:0];
fireDate = [gregorian dateByAddingComponents:dateComponents
toDate:currentDate
options:0];
[localNotification setFireDate:fireDate];
[dateComponents release];
关于特定时间的 iPhone 本地通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8713110/