IOS 10.2 UNUserNotificationCenterDelegate / UNTimeIntervalNotificationTrigger Error?

I am experiencing a strange error with iOS 10.2 with UNTimeIntervalNotificationTriggerand UNUserNotificationCenterDelegate. Basically the notification that I create is immediately received by the delegate, and then again on the correct internal. This only happens when the repeats property is set to true on the trigger.

Has anyone else seen this problem? Now I think that I need to check the trigger date in the delegate and compare it with the saved registered date - but I want to avoid it if possible.

Sample code for creating a notification

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 starts immediately after .add

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

If I change the trigger to repeat false, this does not happen

  let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: false)
+4
1

/ - . ( , ).

, . :

scheduledTimes[identifier] = CACurrentMediaTime()
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

, , :

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    let identifier = notification.request.identifier

    // Required for 10.2?
    // Adding a notification request to the notification center immediately fires this delegate when the trigger is set to repeat
    if let scheduledTime = scheduledTimes[identifier] {
        if CACurrentMediaTime() - scheduledTime < 1.0 {
            completionHandler([])
            return
        }
    }

    // Parse the notification into an internal notification and show it

    completionHandler([])
}

, - 0,04 .

0

Source: https://habr.com/ru/post/1665773/


All Articles