2

到目前为止,在添加 aUNNotificationRequestUNUserNotificationCenter包含之后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中找不到所需的属性。

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

4

1 回答 1

3

I get the time interval value from pending UNNotificationRequest Swift4 code

UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: {requests -> () in
for request in requests {
    let localTrigger = request.trigger  as! UNTimeIntervalNotificationTrigger
    localTimeInterval = localTrigger.timeInterval
    print (localTimeInterval)
}})
于 2018-03-29T17:34:24.480 回答