到目前为止,在向包含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中找到所需的属性。
问题可能是有什么方法可以访问它吗?如果不是,那么用一个合乎逻辑的方法来实现它呢?
发布于 2018-03-29 17:34:24
我从挂起的UNNotificationRequest Swift4代码中获得时间间隔值。
UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: {requests -> () in
for request in requests {
let localTrigger = request.trigger as! UNTimeIntervalNotificationTrigger
localTimeInterval = localTrigger.timeInterval
print (localTimeInterval)
}})https://stackoverflow.com/questions/44116585
复制相似问题