2016-12-22 5 views
0

通知をスケジュールしようとするときに非常に奇妙なエラーが発生しました。ユーザーは、ローカル通知を希望する時と分を提供し、毎週または毎月毎日スケジュールするかどうかを選択できます。リピートが毎日に設定されていると、ローカル通知が実行されない

しかし、私は非常に奇妙な問題に遭遇しました。これは私のコードです:それは、私が例えば12:00に、「毎日」の通知を作成したい場合は

func enableLocalNotifications() { 


    notificationsSwitch.isOn = true 
    notificationsOn = true 

    //first cancel all current local notifications 
    UIApplication.shared.cancelAllLocalNotifications() 

    //create new one according to input 

    let calendar = Calendar(identifier: .gregorian) 
    var fireComponents=calendar.dateComponents([.day, .month, .year], from:NSDate() as Date) 

    fireComponents.hour = notificationHour 
    fireComponents.minute = notificationMinute 
    fireComponents.second = 00 


    fireComponents.calendar = calendar 

    let notification = UILocalNotification() 
    notification.timeZone = NSTimeZone.local 
    notification.alertBody = notificationDescription 
    notification.alertTitle = "Reminder" 
    notification.fireDate = fireComponents.date 


    notification.soundName = UILocalNotificationDefaultSoundName 

    switch notificationRepeat { 
     case "Daily": 
      notification.repeatInterval = NSCalendar.Unit.minute 
     case "Weekly": 
      notification.repeatInterval = NSCalendar.Unit.weekOfYear 
     case "Monthly": 
      notification.repeatInterval = NSCalendar.Unit.month 
     default: 
      break 
    } 

    UserDefaults.standard.setValue(notificationRepeat, forKey: "notificationRepeat") 
    UserDefaults.standard.setValue(notificationDescription, forKey: "notificationDescription") 
    UserDefaults.standard.setValue(notificationHour, forKey: "notificationHour") 
    UserDefaults.standard.setValue(notificationMinute, forKey: "notificationMinute") 
    UserDefaults.standard.setValue(true, forKey: "notificationSet") 


} 

}

面白いことに、で、現時点ではそれが午前11時59分です12時に通知を発しません。実際には何も起きません。しかし、私がrepeatIntervalをNSCalendar.Unit.minuteに変更すると、突然、私が12:00の通知をスケジュールし、11:59であると、私は実際に12:00にそれを受け取ることに気付きました。

バグの修正はありますか?

答えて

0

通知の日付をに設定しているかどうかを再度確認してください。タイムゾーンを適切に設定しないと、日時が変わることがあります。

+0

こんにちは、悲しいことに、それは問題ではありませんでしたが、私はすぐ後にそれを理解しました。私はアプリケーションの中にいて、ローカルの通知が来るのを待っていました。しかし、すぐに私は、アプリケーションを閉じる/それをバックグラウンドで実行させる、私は正しい時刻にすべてのローカル通知を得た。しかし、ユーザーがアプリケーションの内部にいるときにローカルの通知が発せられたかどうかをどのように認識することができますか? – Misha

関連する問題