我正在尝试了解如何在每天重复本地通知时更新消息。
目前我的 AppDelegate 中有以下代码:
func scheduler(at date: Date, numOfNotes: Int)
{
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: date)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, hour: components.hour, minute: components.minute)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: true)
content.badge = numOfNotes as NSNumber
content.body = "REMINDER: " + String(numOfNotes) + " needs to be looked at!"
content.sound = UNNotificationSound.default()
let request = UNNotificationRequest(identifier: "reminderNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request) {(error) in
}
}
我存储numOfNotes
在UserDefaults
. 我的 中有一个,UISwitch
在UITableViewCell
打开时调用该scheduler
函数,如下所示:
func remindMeSwitch(_ remindMeSwitch: UISwitch)
{
numOfNotes = UserDefaults.standard.integer(forKey: "Notes")
let delegate = UIApplication.shared.delegate as? AppDelegate
delegate?.scheduler(at: time, numOfNotes: numOfNotes)
}
但是,当将repeats
参数设置true
为让通知在指定时间每天重复时,numOfNotes
只会调用一次,即当我UISwitch
打开时。
如何将通知设置为每天提醒,但仍可以根据需要更新通知消息?
谢谢。