简体   繁体   中英

How to get the remaining time of pending UNNotificationRequest?

So far, after adding a UNNotificationRequest to the UNUserNotificationCenter that contains UNTimeIntervalNotificationTrigger (Here is the code that I implemented):

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

It works -fine- as it should, but what I need is to access the remaining time for firing the request trigger.

For clarity, in my code snippet, I declared:

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

So, if I wait 6 seconds (starting from executing the above code snippet), the remaining time should be 4 seconds.

Also, I tried to fetch the pending requests to check their triggers, as follows:

UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: { requests in
     for request in requests {
         print(request.trigger)
     }
})

but couldn't find the desired property in UNNotificationTrigger .

The question might be is there even a way to access it? and if it's not, what about a logical workaround to achieve it?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM