How to get the remaining time of pending UNNotificationRequest?

1.7k views Asked by At

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?

1

There are 1 answers

0
Evgeny  Lisin On

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