每隔30分钟发出重复间隔的问题

Mc.*_*ver 6 iphone xcode ios uilocalnotification

我试图每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];
Run Code Online (Sandbox Code Playgroud)

yuj*_*uji 13

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];
Run Code Online (Sandbox Code Playgroud)