首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取挂起UNNotificationRequest的剩余时间?

如何获取挂起UNNotificationRequest的剩余时间?
EN

Stack Overflow用户
提问于 2017-05-22 15:17:11
回答 1查看 1.5K关注 0票数 2

到目前为止,在向包含UNNotificationRequestUNUserNotificationCenter添加了一个UNTimeIntervalNotificationTrigger之后(下面是我实现的代码):

代码语言:javascript
复制
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
    if granted {
        let content = UNMutableNotificationContent()
        center.delegate = self
        content.title = "title"
        content.body = "body"
        
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10.0, repeats: false)
        let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
        center.add(request)
    }
}

它可以正常工作,但我需要的是访问触发请求触发器的剩余时间。

为了清晰起见,在我的代码片段中,我声明:

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10.0, repeats: false)

因此,如果我等待6秒(从执行上面的代码片段开始),剩下的时间应该是4秒。

另外,我试图获取挂起的请求以检查它们的触发器,如下所示:

代码语言:javascript
复制
UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: { requests in
     for request in requests {
         print(request.trigger)
     }
})

但无法在UNNotificationTrigger中找到所需的属性。

问题可能是有什么方法可以访问它吗?如果不是,那么用一个合乎逻辑的方法来实现它呢?

EN

回答 1

Stack Overflow用户

发布于 2018-03-29 17:34:24

我从挂起的UNNotificationRequest Swift4代码中获得时间间隔值。

代码语言:javascript
复制
UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: {requests -> () in
for request in requests {
    let localTrigger = request.trigger  as! UNTimeIntervalNotificationTrigger
    localTimeInterval = localTrigger.timeInterval
    print (localTimeInterval)
}})
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44116585

复制
相关文章

相似问题

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