我遇到一個奇怪的錯誤與iOS 10.2與UNTimeIntervalNotificationTrigger
和UNUserNotificationCenterDelegate
。基本上,我創建的通知會立即被代表接收,然後再次在正確的內部接收。只有在觸發器上的repeats屬性設置爲true時纔會發生這種情況。iOS 10.2 UNUserNotificationCenterDelegate/UNTimeIntervalNotificationTrigger Bug?
有其他人看過這個問題嗎?現在我想我需要檢查委託中的觸發日期,並與存儲的註冊日期進行比較 - 但如果可能的話,我想避免這種情況。
示例代碼來創建通知
let content = UNMutableNotificationContent()
content.body = "My notification message"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let request = UNNotificationRequest(identifier: "MY_IDENTIFIER", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
UNUserNotificationCenterDelegate是,如果我改變觸發重複錯誤後。新增
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// Code called here instantly when trigger repeats = true
// Code called again at proper interval as well (60 seconds)
}
直接燃燒,這種情況不會發生
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)
Docs說:「如果重複爲true,那麼timeInterval參數中的值必須是60秒或更長。」讓我想知道,選擇一個更大的時間間隔並且看看會發生什麼情況會更好,因爲你是對的,就像那樣。 – matt
它實際上設置了多天,我只是將其更改爲60代碼示例。 –