2016-07-01 53 views
2

我想爲我的應用程序設置每日通知系統。它應該每天兩次通知用戶,一次在上午8:00(嘿,那裏,你的早餐劑量已經準備好了,想看看嗎?),並且在晚上7點(Ssup!你的晚上劑量在內部等待) 。我知道通過這樣做,我將在一個月內用完通知,因爲有64個通知上限(用於本地通知),但到那時,應用程序將實時生效,並且我將完成設置遠程通知更新。在當天的特定時間安排本地通知

我看過這個問題:How to schedule a same local notification in swift和.Day都很好。我只需要知道如何在特定時間一天做兩次;提前致謝!

編輯:如何設置通知的具體時間是問題的一部分。 NSDate API給了我有趣的時間間隔(sinceNow/Since1973)。我只是想在下午7點開火。 : - /似乎不能用這些NSDate API來做,除非我錯過了一些東西。

回答

2
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    // Override point for customization after application launch. 
    let settings = UIUserNotificationSettings(forTypes: .Badge, categories: nil) 
    UIApplication.sharedApplication().registerUserNotificationSettings(settings) 

    let localNotification1 = UILocalNotification() 
    localNotification1.alertBody = "Your alert message 111" 
    localNotification1.timeZone = NSTimeZone.defaultTimeZone() 
    localNotification1.fireDate = self.getEightAMDate() 
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification1) 

    let localNotification2 = UILocalNotification() 
    localNotification2.alertBody = "Your alert message22" 
    localNotification2.timeZone = NSTimeZone.defaultTimeZone() 
    localNotification2.fireDate = self.getSevenPMDate() 
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification2) 
    return true 
} 

func getEightAMDate() -> NSDate? { 
    let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) 
    let now: NSDate! = NSDate() 

    let date10h = calendar.dateBySettingHour(8, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)! 
    return date10h 
} 

func getSevenPMDate() -> NSDate? { 
    let calendar: NSCalendar! = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) 
    let now: NSDate! = NSDate() 

    let date19h = calendar.dateBySettingHour(19, minute: 0, second: 0, ofDate: now, options: NSCalendarOptions.MatchFirst)! 
    return date19h 
} 
+0

嘿!感謝您的回覆....快一個...希望你能告訴我如何將時間設置爲8:00 AM/7 PM,因爲NSDate API沒有給我一個直接設置時間的選項。只有'timeIntevalSinceNow/1970 /參考'等。或者我應該使用其中之一?如果是的話,我該怎麼辦呢?謝謝 –

+0

我已經更新了答案並嘗試過。它的工作原理 – ronan

+0

感謝一羣夥伴。我想日曆是我不知道的。看着周圍,有幾個人有同樣的問題。顯然日曆API不會在文檔中更新。但我必須證實這一點。無論如何,你是英雄隊友! –

相關問題