特定时间的 iPhone 本地通知

标签 iphone notifications

我想每周晚上 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/

相关文章:

iphone - UIFont 大小 : half point sizes possible?

javascript - noty Jquery 插件超时没有发生

代码更新后启用 iOS 8 的设备未收到推送通知

javascript - 异常大的 WebKit 通知

Javascript - 在 Android 设备上单击服务工作人员通知后打开浏览器选项卡

ios - 从通知中心移除 “local or remote notifications”

ios - 用于多个/关系下拉选择器的 Swift iOS 解决方案

iphone - 尝试使用 NSLog() 时出现意外错误

iphone - 利用本周和周末在 2 个 NSDate 之间签到

iphone - 在 Mpmovieplayer 或 Mpmusicplayer ios 中播放 youtube 视频