gpt4 book ai didi

ios - 除非 repeats 为真,否则不会存储 UNCalendarNotificationTrigger

转载 作者:搜寻专家 更新时间:2023-10-31 22:29:54 25 4
gpt4 key购买 nike

我注意到,如果我创建一个带有自定义日期的 UNCalendarNotificationTrigger,它不会被添加,除非我输入:让 trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: **true**)

苹果的例子是:

let date = DateComponents()
date.hour = 8
date.minute = 30
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)

repeats == true 是有意义的。

在我的场景中,我不需要创建一个重复多次的通知,但我需要在特定日历日期(当然是将来)仅触发一次的多个通知。

如果我在做:

let calendar = Calendar(identifier: .gregorian)

let formatter = DateFormatter()
formatter.dateFormat = "yyyyMMdd"
let newdate = formatter.date(from: "20161201")

let components = calendar.dateComponents(in: .current, from: newdate!)

let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)

然后我总是收到 0 个待处理通知...

    UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: { (notifications) in
print("num of pending notifications \(notifications.count)")

})

num of pending notification 0

有什么想法吗?

编辑 1:添加其中一个答案指出的其他上下文。我实际上是将请求添加到当前的 UNUserNotificationQueue。

 let request = UNNotificationRequest(identifier: "future_calendar_event_\(date_yyyyMMdd)", content: content, trigger: trigger)

UNUserNotificationCenter.current().add(request) { error in
if let error = error {
// Do something with error
print(error.localizedDescription)
} else {
print("adding \((request.trigger as! UNCalendarNotificationTrigger).dateComponents.date)")
}
}

最佳答案

我也遇到了同样的问题,现在解决了。这是由于 dateComponents 的年份。

为了解决这个问题,我测试了如下代码:

1.

let notificationCenter = UNUserNotificationCenter.current()
let notificationDate = Date().addingTimeInterval(TimeInterval(10))
let component = calendar.dateComponents([.year,.day,.month,.hour,.minute,.second], from: notificationDate)
print(component)
let trigger = UNCalendarNotificationTrigger(dateMatching: component, repeats: false)
let request = UNNotificationRequest(identifier: item.addingDate.description, content: content, trigger: trigger)
self.notificationCenter.add(request){(error) in
if let _ = error {
assertionFailure()
}
}

在控制台中,打印组件:

year: 106 month: 2 day: 14 hour: 12 minute: 3 second: 42 isLeapMonth: false 

并且在这种情况下,在待处理通知列表中找不到该通知。

2.当我将组件的年份明确设置为2017时:

let notificationDate = Date().addingTimeInterval(TimeInterval(10))
var component = calendar.dateComponents([.year,.day,.month,.hour,.minute,.second], from: notificationDate)
component.year = 2017
print(component)
let trigger = UNCalendarNotificationTrigger(dateMatching: component, repeats: false)
let request = UNNotificationRequest(identifier: item.addingDate.description, content: content, trigger: trigger)
self.notificationCenter.add(request){(error) in
if let _ = error {
assertionFailure()
}
}

在控制台中,组件是:

year: 2017 month: 2 day: 14 hour: 12 minute: 3 second: 42 isLeapMonth: false 

然后可以在待处理通知列表中找到此通知。

接下来,我检查挂起的通知请求以查找触发日期的年份部分是 106 还是 2017:

notificationCenter.getPendingNotificationRequests(){[unowned self] requests in
for request in requests {
guard let trigger = request.trigger as? UNCalendarNotificationTrigger else {return}
print(self.calendar.dateComponents([.year,.day,.month,.hour,.minute,.second], from: trigger.nextTriggerDate()!))
}
}

我发现触发器的 nextTriggerDate 组件是:

year: 106 month: 2 day: 14 hour: 12 minute: 3 second: 42 isLeapMonth: false 

结论

所以如果你想将触发器的重复设置为假,你应该确保触发日期大于当前日期。

默认的 dateComponents 年份可能不合适,例如 106。如果您希望通知在 2017 年触发,则应将组件年份显式设置为 2017。

也许这是一个错误,因为我将触发器的 dateComponents 年份设置为 2017,但在待处理通知请求的 nextTriggerDate 中得到 106。

关于ios - 除非 repeats 为真,否则不会存储 UNCalendarNotificationTrigger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40624120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com