3
我有一個提醒應用程序,通過搜索數組發送提醒採取藥物。當通知通過時,我得到了多個通知的海洋,似乎瞬間消失,然後1仍然在最後。Swift:本地通知似乎重複,然後一些消失
我的初始視圖控制器是UITableViewController
。我試過將fireNotifications
函數放在viewDidLoad
中,但它似乎沒有發送任何通知。如果我將fireNotifications
函數調用放在其中一個tableView函數中(即numberOfSectionsInTableView
),我可以獲得通知,但它們有我在上面提到的奇怪行爲。
我的代碼是:
func fireNotifications() {
if NSUserDefaults().boolForKey("NotifySwitch") { //checks settings to make sure user wants reminders
for (index, items) in myMedsList.enumerate() {
let notification = UILocalNotification()
notification.fireDate = NSDate(timeIntervalSinceNow: 20)
notification.alertBody = "Reminder: Take your \(items.name) now!"
notification.alertAction = "Let's take my meds!"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["Med Index": index]
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
}
}
上午我做在我的函數調用的錯誤呢?或者代碼中有什麼錯誤?值得注意的是,我只有20秒的fireDate用於測試目的,但是計劃爲myMedsList數組中的每個項目定製fireDate。
添加藥物時,爲陣列每個項目安排一次更好嗎?然後,我需要在每次編輯藥物時編輯通知,這會更加複雜。用戶可以編輯每種藥物的每日時間,並且需要更改通知。因此,我在加載應用程序時讓它們全部運行。將需要取消之前這樣做,但這不應該改變我與測試有關的問題,因爲他們都只會觸發一次。 – LearningSwift
更新 - 你完全正確。我在運行for循環之前調用了它,並且現在他們正確地通過了。謝謝! – LearningSwift
我很樂意提供幫助! –