nextTriggerDate() doesn't return the 'expected' value, is there another way to obtain the next fire date of a repeating Local Notification?

后端 未结 1 998
死守一世寂寞
死守一世寂寞 2021-01-25 15:31

In my application I allow users to schedule repeating Local Notifications. The issue that I have though (any many others based on looking around) is that nextTriggerDate() alway

相关标签:
1条回答
  • 2021-01-25 16:14

    Confirmed. I ran this code:

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 120, repeats: true)
    print("scheduling at", Date())
    DispatchQueue.main.asyncAfter(deadline: .now()+15) {
        print("checking at", Date())
        UNUserNotificationCenter.current().getPendingNotificationRequests {
            arr in let arr = arr
            if let req = arr[0].trigger as? UNTimeIntervalNotificationTrigger {
                let fd = req.nextTriggerDate()
                print("trigger date", fd as Any)
            }
        }
    }
    // ... proceed to configure and schedule the notification
    

    I also configured my user notification center delegate to receive the notification in the foreground and print the time.

    Here's what I saw in the console:

    scheduling at 2018-08-01 03:40:36 +0000
    checking at 2018-08-01 03:40:51 +0000
    trigger date Optional(2018-08-01 03:42:51 +0000)
    received notification while active 2018-08-01 03:42:36 +0000
    

    So the trigger date was reported as 2 minutes from when I checked, but the notification actually fired 2 minutes from when I scheduled it.

    I'd describe that as a bug!

    The one disagreement I'd have with your original question is that I get exactly the same result for a non-repeating UNTimeIntervalNotificationTrigger:

    scheduling at 2018-08-01 03:45:50 +0000
    checking at 2018-08-01 03:46:06 +0000
    trigger date Optional(2018-08-01 03:48:06 +0000)
    received notification while active 2018-08-01 03:47:50 +0000
    

    A UNTimeIntervalNotificationTrigger also has a timeInterval property, but even that does us no good unless we know when the notification was originally scheduled — and a UNNotificationRequest provides no way to find that out.

    (Just to be certain, I postponed my checking until the repeating notification had fired a couple of times, but the result was the same: nextTriggerDate is clearly adding the timeInterval to now rather than reporting the time at which the notification will next fire.)

    0 讨论(0)
提交回复
热议问题