[英]How to get the remaining time of pending UNNotificationRequest?
到目前为止,加入后UNNotificationRequest
到UNUserNotificationCenter
包含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)
}
}
它可以正常工作,但我需要的是访问剩余时间来触发请求触发器。
为了清楚起见,在我的代码段中,我声明了:
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10.0, repeats: false)
因此,如果我等待6秒(从执行上述代码段开始),则剩余时间应为4秒。
另外,我尝试获取待处理的请求以检查其触发器,如下所示:
UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: { requests in
for request in requests {
print(request.trigger)
}
})
但在UNNotificationTrigger中找不到所需的属性。
问题可能是什至有一种访问方法? 如果不是,那么实现该目标的逻辑解决方法呢?
我从待处理的UNNotificationRequest Swift4代码中获取时间间隔值
UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: {requests -> () in
for request in requests {
let localTrigger = request.trigger as! UNTimeIntervalNotificationTrigger
localTimeInterval = localTrigger.timeInterval
print (localTimeInterval)
}})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.