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

时间:2012-02-06 15:44:06

标签: iphone ios xcode 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];

1 个答案:

答案 0 :(得分:13)

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

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

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