簡體   English   中英

如何獲取待處理的UNNotificationRequest的剩余時間?

[英]How to get the remaining time of pending UNNotificationRequest?

到目前為止,加入后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)
    }
}

它可以正常工作,但我需要的是訪問剩余時間來觸發請求觸發器。

為了清楚起見,在我的代碼段中,我聲明了:

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

因此,如果我等待6秒(從執行上述代碼段開始),則剩余時間應為4秒。

另外,我嘗試獲取待處理的請求以檢查其觸發器,如下所示:

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

但在UNNotificationTrigger中找不到所需的屬性。

問題可能是什至有一種訪問方法? 如果不是,那么實現該目標的邏輯解決方法呢?

我從待處理的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM