每 30 分钟重复间隔有问题
我试图每 30 分钟重复一次本地通知,但我的代码无法正常工作...如果您帮助我并找到解决方案,我将不胜感激,这是我的代码:
UILocalNotification *reminderNote = [[UILocalNotification alloc]init];
reminderNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:60 * 30];
reminderNote.repeatInterval = NSHourCalendarUnit;
reminderNote.alertBody = @"some text";
reminderNote.alertAction = @"View";
reminderNote.soundName = @"sound.aif";
[[UIApplication sharedApplication] scheduleLocalNotification:reminderNote];
I am trying to repeat a local notification every 30 minutes but my code does not work fine ... I would be grateful if you help me and find the solution , here is my code :
UILocalNotification *reminderNote = [[UILocalNotification alloc]init];
reminderNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:60 * 30];
reminderNote.repeatInterval = NSHourCalendarUnit;
reminderNote.alertBody = @"some text";
reminderNote.alertAction = @"View";
reminderNote.soundName = @"sound.aif";
[[UIApplication sharedApplication] scheduleLocalNotification:reminderNote];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
firedate
设置通知第一次触发的时间,repeatInterval
是通知重复之间的时间间隔。因此,问题中的代码安排从现在起 30 分钟(60 * 30 秒)后触发通知,然后每小时重复一次。不幸的是,您只能安排通知以 NSCalendar 定义的精确时间间隔重复 常量:例如,每分钟、每小时,每天、每月,但不是这些时间间隔的倍数。
幸运的是,要每 30 分钟收到一次通知,您只需安排两个通知:一个立即通知,一个 30 分钟后通知,并且每小时重复一次。就像这样:
firedate
sets the time that the notification fires the first time, andrepeatInterval
is the interval between between repetitions of the notification. So the code in the question schedules a notification to fire 30 minutes (60 * 30 seconds) from now, and then repeat every hour.Unfortunately, you can only schedule notifications to repeat at exact intervals defined by NSCalendar constants: e.g., every minute, every hour, every day, every month, but not at multiples of those intervals.
Luckily, to get a notification every 30 minutes, you can just schedule two notifications: one right now, one 30 minutes from now, and have both repeat every hour. Like so: