andre
andre

Reputation: 883

Is there a way to deliver a local notification instantly in Swift?

I have a function and want to deliver a local notification when that function is executed. For now I've been using a Time interval notification trigger and had set the interval to 0.1s, but this doesn't feel like a proper way to deliver notification instantly because it still has a delay of 0.1s, even though it's barely noticeable.

My question is that is there a way to deliver a notification instantly, like without any delay? I've been looking for a solution to this problem but haven't found any, hope you guys could help. Thanks.

func didUpdateSettings() {
    //other code...
    let content = UNMutableNotificationContent()

    content.title = "Notification title"
    content.subtitle = "Notification subtitle"
    content.body = "Notification body"
    content.sound = UNNotificationSound.default

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
    let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}

Upvotes: 8

Views: 4185

Answers (1)

alxlives
alxlives

Reputation: 5212

Acording with the documentation:

https://developer.apple.com/documentation/usernotifications/unnotificationrequest/1649633-init

trigger The condition that causes the notification to be delivered. Specify nil to deliver the notification right away.

So the code would be:

let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: nil)

Upvotes: 28

Related Questions