首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检索UNNotificationRequest的下一个请求日期和相关ID

检索UNNotificationRequest的下一个请求日期和相关ID
EN

Stack Overflow用户
提问于 2018-04-05 16:12:29
回答 1查看 245关注 0票数 1

更新:

我需要找到下一个通知请求和相关的ID,因此我最终采用了以下方法:

代码语言:javascript
复制
    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是不可比拟的:

代码语言:javascript
复制
requests.min(by: (UNNotificationRequest, UNNotificationRequest) throws -> Bool>)

如果有人有更好的解决办法,那就告诉我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-05 18:27:19

我认为Sequence对于符合Comparable协议的对象序列有一个min()函数。我不认为UNNotificationRequest对象是可比较的,因此不能直接在UNNotificationRequest对象数组上使用min()

您必须首先使用flatMap将通知数组转换为非零触发器日期数组:

代码语言:javascript
复制
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)
}

(该代码可能需要稍加调整才能编译,但这是基本思想。)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49676957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档