Home:ALL Converter>How to show a local notification when the user clicks a button on iOS?

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

Ask Time:2022-03-11T04:07:36         Author:matteoh

Json Formatter

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.

Author:matteoh,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/71430181/how-to-show-a-local-notification-when-the-user-clicks-a-button-on-ios
yy