xcode 每 30 分钟出现一次重复间隔问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9162737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:14:12  来源:igfitidea点击:

Having issue with repeatinterval every 30 minutes

iphoneiosxcodeuilocalnotification

提问by Mc.Lover

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 :

我试图每 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];

回答by yuji

firedatesets the time that the notification fires the first time, and repeatIntervalis 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.

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

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.

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

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:

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