如何获取待处理UNNotificationRequest的剩余时间?

时间:2017-05-22 15:17:12

标签: ios swift notifications unnotificationrequest unnotificationtrigger

到目前为止,将UNNotificationRequest添加到包含UNUserNotificationCenter的{​​{1}}后(以下是我实施的代码):

UNTimeIntervalNotificationTrigger

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

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

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) } }

所以,如果我等待 6 秒(从执行上面的代码片段开始),剩余时间应为 4 秒。

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

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

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

问题可能是甚至有办法访问它?如果不是,那么实现它的逻辑解决方法呢?

1 个答案:

答案 0 :(得分:0)

我从待处理的UNNotificationRequest获取时间间隔值 Swift4代码

UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: {requests -> () in
for request in requests {
    let localTrigger = request.trigger  as! UNTimeIntervalNotificationTrigger
    localTimeInterval = localTrigger.timeInterval
    print (localTimeInterval)
}})