2016-07-01 9 views
2

私のアプリの毎日の通知システムを設定したいと思います。それは1日2回、午前8時に1回、午前7時に1回(あなたの夕食は中を待っています) 。私はこれを行うことによって、64ヶ月間の通知キャップ(ローカル通知用)があるので1ヶ月後に通知がなくなることを知っていますが、それまでにアプリが公開され、リモート通知の更新を設定することになります。当日の特定の時刻にローカル通知をスケジュールする

私はこの質問を見てきました:How to schedule a same local notification in swiftと.Dayはすべていいです。私はちょうどそれらの指定された時間にそれを1日2回行う方法を知る必要があります。前もって感謝します!

編集:通知の特定の時刻を設定する方法は、問題の一部です。 NSDate APIは面白い時間間隔を与えています(SinceNow/Since 1973年)。私は午後7時にそれを放つように設定したい。 : - /私は何かを欠場していない限り、これらのNSDate apisでそれを行うように見えることはできません。

答えて

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

hey!返事をお寄せいただきありがとうございます....クイックワン...時間を午前8時に設定する方法を教えて欲しいと思っていました。なぜなら、NSDate APIは時間を直接設定するオプションを与えないからです。 「timeIntevalSinceNow/1970/reference」などしか使用できません。それらのうちの1つを使用するはずですか?もしそうなら、私はそれについてどうやって行くのですか?ありがとう –

+0

私は答えを更新し、試しました。それは動作します – ronan

+0

おかげで仲間。カレンダーは私が知らなかったものだと思います。周りを見回し、いくつかの人々が同じ問題を抱えていました。明らかに、カレンダーAPIはドキュメントでは更新されていません。しかし、私はそれを確認する必要があります。とにかく、あなたは英雄です! –

関連する問題