更新:
我需要找到下一个通知请求和相关的ID,因此我最终采用了以下方法:
UNUserNotificationCenter.current().getPendingNotificationRequests {
(requests) in
var requestDates = [String:Date]()
for request in requests{
let requestId = request.identifier
let trigger = request.trigger as! UNCalendarNotificationTrigger
let triggerDate = trigger.nextTriggerDate()
requestDates[requestId] = triggerDate
}
let nextRequest = requestDates.min{ a, b in a.value < b.value }
print(String(describing: nextRequest))
}我认为这个方法可能会提供一个更优雅的解决方案,但正如邓肯在下面指出的那样,UNNotificationRequests是不可比拟的:
requests.min(by: (UNNotificationRequest, UNNotificationRequest) throws -> Bool>)如果有人有更好的解决办法,那就告诉我。
发布于 2018-04-05 18:27:19
我认为Sequence对于符合Comparable协议的对象序列有一个min()函数。我不认为UNNotificationRequest对象是可比较的,因此不能直接在UNNotificationRequest对象数组上使用min()。
您必须首先使用flatMap将通知数组转换为非零触发器日期数组:
UNUserNotificationCenter.current().getPendingNotificationRequests { requests in
//map the array of `UNNotificationRequest`s to their
//trigger Dates and discard any that don't have a trigger date
guard let firstTriggerDate = (requests.flatMap {
$0.trigger as? UNCalendarNotificationTrigger
.nextTriggerDate()
}).min() else { return }
print(firstTriggerDate)
}(该代码可能需要稍加调整才能编译,但这是基本思想。)
https://stackoverflow.com/questions/49676957
复制相似问题