简体   繁体   English

如何获取待处理的UNNotificationRequest的剩余时间?

[英]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): 到目前为止,加入后UNNotificationRequestUNUserNotificationCenter包含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)
    }
}

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. 因此,如果我等待6秒(从执行上述代码段开始),则剩余时间应为4秒。

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 . 但在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 我从待处理的UNNotificationRequest Swift4代码中获取时间间隔值

UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: {requests -> () in
for request in requests {
    let localTrigger = request.trigger  as! UNTimeIntervalNotificationTrigger
    localTimeInterval = localTrigger.timeInterval
    print (localTimeInterval)
}})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NSURLConnection如何获取剩余时间信息 - NSURLConnection how to get time remaining information 如何从异步请求中获取剩余的上传时间? - How to get Upload time remaining from Asynchronous Request? 如何获取使用MPMusicPlayerController播放的正在播放的歌曲的剩余时间? - how to get the remaining time of the Playing song which is played using MPMusicPlayerController? 如何获取在AVAudioPlayer中播放的歌曲的剩余时间 - how to get the remaining time of the song which is played in AVAudioPlayer UNNotificationRequest 每天在特定时间发送本地通知 - UNNotificationRequest to send local notification daily at a specific time 如何使用 for 循环获取 UNNotificationRequest 数组以填充 SwiftUI 列表视图? - How can I get UNNotificationRequest array using for loop to populate SwiftUI List view? 如何找到剩余的上传时间 - How to find remaining time for uploading 如何立即弹出UNNotificationRequest? - How can I make UNNotificationRequest pop immediately? 如何设置UNNotificationRequest的自定义重复间隔? - How to set UNNotificationRequest's custom repeat interval? 如何访问非重复NSTimer的剩余时间 - How to access the remaining time of a non repeating NSTimer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM