2016-02-04 37 views
0

我想运行UILocalNotifications和重复,每2分钟。为此我做:为什么UILocalNotifications不会重复?

let reminderNote: UILocalNotification = UILocalNotification() 
reminderNote.fireDate = NSDate(timeIntervalSinceNow: 60 * 2) 
reminderNote.repeatInterval = NSCalendarUnit.Hour 
reminderNote.alertBody = "some text" 
reminderNote.alertAction = "View" 

UIApplication.sharedApplication().scheduleLocalNotification(reminderNote) 

它运行它只是一次,后来它没有。

我想这是因为这行:

reminderNote.repeatInterval = NSCalendarUnit.Hour 

我怎么能重复我的通知每1.5或2小时?

+0

[UILocalNotification重复时间间隔自定义警报(太阳,星期一,星期三,星期四,星期五,星期五,星期六)可能的重复](http://stackoverflow.com/questions/6966365/uilocalnotification-repeat-interval-for-自定义报警阳光周一周二,周三,周四-F) –

+0

@OlegGordiichuk这些帖子不帮我,所以这就是为什么我已经打开了一个新问题 –

+0

也许你可以安排它调用的方法中的通知, ,安排另一个... –

回答

1

直接你不能。

不幸的是repeatInterval只能设置为TimeUnitHourMinuteSecond

例如:比方说,如果你想重复通知,每次2分钟,你将需要创建30通知该每小时重复。

+0

它没有用= /还有什么可以推荐我吗?我认为使用推送通知无助于我的情况。我需要在用户设置的几个小时内显示通知。所以它可以是每小时,2小时,5小时等。 –

+0

没有我知道的其他选项。你只需要这样管理你的代码。 – rptwsthi

0

firedate设置该通知触发所述第一时间的时间,和按repeatInterval是所述通知的重复之间的时间间隔。

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

幸运的是,要每隔2分钟收到一个通知,您可以安排29个通知:一个现在,一个2分钟后,一个2分钟 - 先前的29个通知时间表,每小时重复一次。像这样: 因此,代码中的问题的时间表通知到每2分钟(60 * 2秒)从现在火了,然后重复每隔一小时。

UILocalNotification *reminderNote = [[UILocalNotification alloc]init]; 
reminderNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:60 * 2]; 
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]; 

2小时,您可以设置您的通知,现在,从现在开始2小时,从以前的1个2小时至11通知(12 * 2小时)= 24后 - 第2小时值(因为一日一会当天改变)= 22/2 = 11通知将需要在NSDayCalendarUnit的重复时间间隔上设置。