2012-02-06 102 views
6

我想重复一个本地通知,每隔30分钟,但我的代码不能正常工作......我将不胜感激,如果你帮我找到解决办法,这里是我的代码:与按repeatInterval每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]; 

回答

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

谢谢大约每1小时或2或3我应该这样吗?例如每1小时:'reminderNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60]; reminderNote.repeatInterval = NSHourCalendarUnit;'和第二火日期'reminderNote.fireDate = [NSDate的dateWithTimeIntervalSinceNow:60 * 60];' – 2012-02-07 08:15:01

+0

每1个小时,你只需要安排一个通知。只需在我的答案中的代码并摆脱最后两行。如有必要,更改'fireDate',以便它与您想要触发通知的第一次相对应。 – yuji 2012-02-07 09:21:34

+0

大约每2小时或3次?这样对吗 ? :'reminderNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 2]; [[UIApplication的sharedApplication] scheduleLocalNotification:reminderNote];' – 2012-02-07 17:43:46