【发布时间】:2017-10-22 07:56:31
【问题描述】:
到目前为止,在将UNNotificationRequest 添加到包含UNTimeIntervalNotificationTrigger 的UNUserNotificationCenter 之后(这是我实现的代码):
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)
}
}
它可以正常工作,但我需要的是访问触发请求触发器的剩余时间。
为了清楚起见,在我的代码 sn-p 中,我声明:
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10.0, repeats: false)
所以,如果我等待6秒(从执行上述代码sn-p开始),剩余时间应该是4秒。
另外,我尝试获取挂起的请求以检查其触发器,如下所示:
UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: { requests in
for request in requests {
print(request.trigger)
}
})
但在UNNotificationTrigger 中找不到所需的属性。
问题可能是有没有办法访问它?如果不是,那么实现它的合乎逻辑的解决方法呢?
【问题讨论】:
-
可以获取nextTriggerDate,获取日期timeIntervalSinceNow属性
print(request.trigger.nextTriggerDate() ?? "nil") -
@LeoDabus 你能详细说明一下吗?如果没有下一个触发器怎么办?即如果只有一个呢?
-
next trigger 仅返回 NEXT 触发日期。它永远不会返回超过一个
-
试试
print(request.trigger.nextTriggerDate()?.timeIntervalSinceNow ?? "") -
@LeoDabus 非常感谢,它确实有效!但问题是在终止应用程序并重新启动后,似乎通知将重置,即剩余时间将被重置。有什么想法吗?
标签: ios swift notifications unnotificationrequest unnotificationtrigger