【发布时间】:2017-04-12 05:07:01
【问题描述】:
我正在尝试了解如何在每天重复时更新本地通知中的消息。
目前我的 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 中。我的UITableViewCell 中有一个UISwitch,它在打开时会调用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 时。
如何将通知设置为每天提醒,但仍能根据需要更新通知消息?
谢谢。
【问题讨论】:
-
您似乎需要使用适当的消息安排单独的非重复通知来实现这一点。
-
@Losiowaty,我怀疑是这种情况,但我希望每次计数更改时都不必重新安排
-
您可以尝试添加通知服务应用程序扩展,但我不确定它是否会用于本地通知。它也仅适用于 iOS 10。
标签: ios swift swift3 unusernotificationcenter