大佬教程收集整理的这篇文章主要介绍了基于用户在 ToDo DatePicker (Swift & SwiftUI) 中设置的日期时间的 iOS 本地通知,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在通过应用学习,并且已经能够构建一个带有标题、优先级和截止日期的待办事项应用程序,并将其传递给核心数据并将其显示在列表视图中。
此时使用以下代码。我已经能够启动一个弹出窗口,请求用户同意通知。
使用的函数
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)")
}
}
}
关于如何根据每个待办事项截止日期设置本地通知的任何建议或指导?
注意:代码基于网络上提供的各种解决方案。这是一种对我有用到收集用户对通知的批准的方法。
如果没有看到完整的代码和调试到底错误的可能性,很难说,所以我在这里写了一个小例子https://github.com/yellow-cap/swiftui-local-notifications。
要探索的主要文件:LocalNotificationsApp.swift
、NotificationManager.swift
、ContentView.swift
如果您有任何问题,请随时提出。
代码审查后更新:
安排通知操作
func scheduleNotification() {
let timeIntervalminutes = 1
let notificationManager = // get NotificationManager
// create notification content
let content = UNMutableNotificationContent()
content.title = "Example title"
content.body = "Example body"
// create trigger (will show notification 1 min before SELEcted timE)
let triggerDate = self.setReminder.addingTimeInterval(Double(-timeIntervalminutes * 60))
let trigger = UNCalendarNotificationtrigger(
dateMatching: Calendar.current.dateComponents([.timeZone,.year,.month,.day,.hour,.minute],from: triggerDatE),repeats: false
)
notificationManager.scheduleNotification(
id: "Example id",content: content,trigger: trigger)
}
通知管理器调度通知功能:
func scheduleNotification(id: String,content: UNNotificationContent,trigger: UNNotificationtrigger) {
let request = UNNotificationrequest(identifier: id,trigger: trigger)
notificationCenter.add(request)
}
NotificationManager 的完整代码:https://github.com/yellow-cap/swiftui-local-notifications/blob/main/LocalNotifications/LocalNotifications/NotificationManager.swift
,我怀疑它不起作用,因为日期比较语句。
if setReminder < Date() {
if let addedValue = Calendar.current.date(byAdding: .minute,value: 1,to: setReminder) {
setReminder = addedValue
}
}
应该
var setReminder = Date().addingTimeInterval(10)
let comps = Calendar.current.dateComponents([.year,from: setReminder)
以上是大佬教程为你收集整理的基于用户在 ToDo DatePicker (Swift & SwiftUI) 中设置的日期时间的 iOS 本地通知全部内容,希望文章能够帮你解决基于用户在 ToDo DatePicker (Swift & SwiftUI) 中设置的日期时间的 iOS 本地通知所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。