'How to show a local notification when the user clicks a button on iOS?

In my iOS app, I want to show a local notification when the user clicks a button.

But according to the documentation, it looks like the notification can only be displayed:

  • after a certain amount of time or on a specific date
  • when the user enters or exits a specific location
  • when the app gets a push notification

The best I can do so far is:

// content:
let content = UNMutableNotificationContent()
content.categoryIdentifier = "test_category"
content.title = "Title"
content.subtitle = "Subtitle"
content.body = "Notification body"
content.sound = UNNotificationSound.default

// trigger:
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

So how can I show a local notification on iOS when the user clicks a button?

Thanks.



Solution 1:[1]

Just make it 1 second

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sh_Khan