【问题标题】:iOS Local Notification based on date time set by user in ToDo DatePicker (Swift & SwiftUI)基于用户在 ToDo DatePicker(Swift 和 SwiftUI)中设置的日期时间的 iOS 本地通知
【发布时间】: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)")
        }
    }
}

关于如何根据每个待办事项到期日设置本地通知的任何建议或指导?

注意:代码基于网络上可用的各种解决方案。这是对我有用的方法,可以收集用户对通知的批准。

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    如果没有看到完整的代码和调试错误的可能性很难说,所以我在这里写了一个小例子https://github.com/yellow-cap/swiftui-local-notifications

    要探索的主要文件:LocalNotificationsApp.swiftNotificationManager.swiftContentView.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)
    }
    

    通知管理器 scheduleNotification 功能:

        func scheduleNotification(id: String, content: UNNotificationContent, trigger: UNNotificationTrigger) {
            let request = UNNotificationRequest(identifier: id, content: content, trigger: trigger)
            notificationCenter.add(request)
    }
    

    NotificationManager 的完整代码:https://github.com/yellow-cap/swiftui-local-notifications/blob/main/LocalNotifications/LocalNotifications/NotificationManager.swift

    【讨论】:

    • 谢谢@Artem 我会看看你的例子。我还将找到一种方法将我的代码直接发布到 Git 上。
    • 希望对你有帮助github.com/AresZephyr/SirTodoAlot你分享的例子和我研究时发现的很相似。但是,我不确定如何将 与用户提交的表单生成的日期时间联系起来以添加新的列表项。
    • 抱歉,我在您的代码中找不到任何与通知相关的内容。我已经克隆了repo,代码运行了,但是上面发布的代码在哪里呢?
    • 功能位于内容视图的最后,并添加新的待办事项视图。 View 文件夹包含您需要查看的文件。
    • 代码更新现已完成。您应该能够找到所有代码片段。
    【解决方案2】:

    我怀疑它不起作用,因为日期比较语句。

    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, .month, .day, .hour, .minute], from: setReminder)
    

    【讨论】:

    • 感谢@Ernist 您的建议也是我的原始代码。但是,这会在 10 秒后弹出通知。我希望根据在每个任务中设置为截止日期的日期弹出通知。
    • 好的,您只需将 Date().addingTimeInterval(10) 更改为您的任务日期。请注意,通知触发日期必须具有当前日期时间 + 60 秒,并且本地通知也是有限的,可以为应用推送 64 个通知,以免说您有 100 个任务,而不是在这种情况下我不确定您将如何触发通知。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    相关资源
    最近更新 更多