每 30 分钟重复间隔有问题

发布于 2025-01-02 22:12:15 字数 476 浏览 1 评论 0原文

我试图每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筑梦 2025-01-09 22:12:15

firedate 设置通知第一次触发的时间,repeatInterval 是通知重复之间的时间间隔。因此,问题中的代码安排从现在起 30 分钟(60 * 30 秒)后触发通知,然后每小时重复一次。

不幸的是,您只能安排通知以 NSCalendar 定义的精确时间间隔重复 常量:例如,每分钟、每小时,每天、每月,但不是这些时间间隔的倍数。

幸运的是,要每 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];

reminderNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60];
[[UIApplication sharedApplication] scheduleLocalNotification:reminderNote];

firedate sets the time that the notification fires the first time, and repeatInterval 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:

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];

reminderNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60];
[[UIApplication sharedApplication] scheduleLocalNotification:reminderNote];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文