【发布时间】:2021-02-16 12:54:18
【问题描述】:
我通过申请来学习,并且已经能够构建一个带有标题、优先级和截止日期的待办事项应用程序,并将其传递给核心数据并将其显示在列表视图中。
此时使用下面的代码。我已经能够启动一个弹出窗口,请求用户同意通知。
使用的功能
func requestPush() -> Void {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
if success {
print("All set!")
} else if let error = error {
print(error.localizedDescription)
}
}
}
在内容视图中调用
.onAppear(perform: requestPush)
此时,我尝试了几种解决方案,将日期选择器中的日期用于每个待办事项并设置提醒。但是我无法让计划本地通知功能正常工作。
这是我迄今为止一直在使用的。
func scheduleNotification() -> Void{
let content = UNMutableNotificationContent()
content.title = "Your title"
content.body = "Your body"
var setReminder = Date()
if setReminder < Date() {
if let addedValue = Calendar.current.date(byAdding: .minute, value: 1, to: setReminder) {
setReminder = addedValue
}
}
let comps = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: setReminder)
let trigger = UNCalendarNotificationTrigger(dateMatching: comps, repeats: false)
let request = UNNotificationRequest(identifier: "alertNotificationUnique", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
}
关于如何根据每个待办事项到期日设置本地通知的任何建议或指导?
注意:代码基于网络上可用的各种解决方案。这是对我有用的方法,可以收集用户对通知的批准。
【问题讨论】: