2016-02-04 34 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的重複時間間隔上設置。