我有一个提醒应用程序正在搜索数组以发送提醒以服用药物。当通知发出时,我会收到多个通知的海洋,并且似乎瞬间消失,然后1仍然在最后。
我的初始视图控制器是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)
}
}
}
我在函数调用的地方犯了错误吗?或者代码中有什么问题?值得注意的是,我只需将fireDate作为测试目的的20秒,但计划为myMedsList数组中的每个项目自定义fireDate。
答案 0 :(得分:2)
您似乎每次都会添加所有通知。您只应添加之前未添加的通知。如果您不想这样做,可以先致电cancelAllLocalNotifications()
再重新加入。{/ p>