swift 当死线到达时触发本地通知

insrf1ej  于 2022-12-02  发布在  Swift
关注(0)|答案(2)|浏览(195)

在我的待办事项应用程序中,我试图在任务的截止日期到来时设置本地通知,但我不知道日历触发器有什么问题,间隔触发器工作正常。在函数体中,我把默认日期()

func setupNotifications(id: String, contentTitle: String, contentBody: String, date: Date) {
        center.getNotificationSettings { (settings) in
            if (settings.authorizationStatus == .authorized) {
                let content = UNMutableNotificationContent()
                content.title = contentTitle
                content.body = contentBody
                content.sound = .default
                
                
                let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour], from: Date().addingTimeInterval(5))
                let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
                let trigger2 = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
                let request = UNNotificationRequest(identifier: id, content: content, trigger: trigger)
                let request2 = UNNotificationRequest(identifier: id, content: content, trigger: trigger2)
                self.center.add(request)
                }
            }
3bygqnnd

3bygqnnd1#

你说:

let dateComponents = Calendar.current.dateComponents(
    [.year, .month, .day, .hour], from: Date().addingTimeInterval(5)
)

时间间隔是以秒为单位计算的,但是你的dateComponents没有秒;你告诉它只考虑年、月、日和小时。而 * 那个 * 时间已经过去了。
只需将日期组件转换回日期即可看到这一点:

let d = Date()
print(d) // 2022-12-01 14:24:02 +0000
let dateComponents = Calendar.current.dateComponents(
    [.year, .month, .day, .hour],
    from: d.addingTimeInterval(5)
)
let d2 = Calendar.current.date(from: dateComponents)
print(d2) // 2022-12-01 14:00:00 +0000

因此,至少需要在组件列表中包含.seconds。正如您所看到的,这将改变一切:

import UIKit

let d = Date()
print(d) // 2022-12-01 14:25:22 +0000
let dateComponents = Calendar.current.dateComponents(
    [.year, .month, .day, .hour, .second],
    from: d.addingTimeInterval(5)
)
let d2 = Calendar.current.date(from: dateComponents)
print(d2) // 2022-12-01 14:00:27 +0000
ny6fqffe

ny6fqffe2#

我在这里找到了解决方案https://github.com/YoussifHany51/ToDoList/blob/main/ToDoList/Models/localNotification.swift并将其应用到我的项目中

func setupNotifications(id: String, deadline:Date?) {
    if deadline != nil {
        let calendar = Calendar.current
        let hour = calendar.component(.hour, from: deadline ?? Date())
        let minute = calendar.component(.minute, from: deadline ?? Date())
        let second = calendar.component(.second, from: deadline ?? Date())
        let day = calendar.component(.day, from: deadline ?? Date())
        let month = calendar.component(.month, from: deadline ?? Date())
        let content = UNMutableNotificationContent()
        content.title = "To Do"
        content.subtitle = "Today is deadline for: \(id)"
        content.sound = .default
        var operationDate = DateComponents()
        operationDate.hour = hour
        operationDate.minute = minute
        operationDate.second = second
        operationDate.month = month
        operationDate.day = day
        //Trigger
        let trigger = UNCalendarNotificationTrigger(dateMatching: operationDate, repeats: false)
        //Request
        let requst = UNNotificationRequest(
            identifier: id, content: content,trigger: trigger
        )
        UNUserNotificationCenter.current().add(requst)
    }
}

相关问题