2
私は質問する前に検索しましたが、何も見つかりませんでした。私は私のアプリで通知をスケジュールするために、このコードを使用しています:Swiftに追加した後でUILocalNotificationを受け取る
let firstFormatter = NSDateFormatter()
firstFormatter.dateFormat = "MMMM, dd, HH:mm"
let date = "\(dueDateTxtField.text) \(notificationTimeTxtField.text)"
let fireDate = firstFormatter.dateFromString(date)
let realNotification = UILocalNotification()
realNotification.fireDate = fireDate
realNotification.soundName = UILocalNotificationDefaultSoundName
realNotification.alertTitle = "Homework reminder"
realNotification.alertBody = "You have a pending \(subjectTxtField.text) homework for tomorrow."
let secondFormatter = NSDateFormatter()
secondFormatter.dateFormat = "yyyy"
let falseNotification = UILocalNotification()
falseNotification.fireDate = secondFormatter.dateFromString("2020")
falseNotification.soundName = UILocalNotificationDefaultSoundName
falseNotification.alertTitle = "This notification will never get to you"
falseNotification.alertBody = "Never"
if notifyMeSegmentedControl.selectedSegmentIndex == 0 {
UIApplication.sharedApplication().scheduleLocalNotification(realNotification)
} else {
UIApplication.sharedApplication().scheduleLocalNotification(falseNotification)
}
ユーザーが通知を受け取るかどうかを選択することができますので、私はUISegmentedControlを使用しています。問題は、新しい宿題を追加するとすぐに(アプリは課題や説明などを追加する宿題アプリです)、「notificationTimeTxtField」を設定しても既に通知が届きます今はやめろ。
私が間違っていることを教えてください。 ありがとうございます!
編集:私は火事の日付を印刷しようとしましたが、それはNILです。なぜテキストフィールドにテキストがあるのかわかりません!
火災の日付をコンソールに表示すると、それは将来の日付ですか?また、ユーザーが通知を受けたくない場合は、おそらくUIApplication.sharedApplication()を使用したいと思っています。cancelAllLocalNotifications() –
これを試してみました。そんなことがあるものか? –
文字列を正しくフォーマットして日付を変換していません。 dueDateTextFieldとnotificationTimeTextFieldとは何ですか?彼らはあなたの日付フォーマット –