Create local notification in Swift
To create a local notification in Swift, you will need to use the UserNotifications framework. Here is an example of how to do this:
import UserNotifications
// Create the notification content
let content = UNMutableNotificationContent()
content.title = "Title of your notification"
content.body = "Body of your notification"
// Set the trigger for the notification
let date = Date().addingTimeInterval(10) // 10 seconds from now
let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
// Create the request for the notification
let request = UNNotificationRequest(identifier: "YourNotificationIdentifier", content: content, trigger: trigger)
// Add the request to the notification center
UNUserNotificationCenter.current().add(request) { (error) in
if let error = error {
print("Error adding notification request: \(error)")
}
}
This code will create a notification that will be triggered 10 seconds from the time it is added to the notification center. When the notification is triggered, it will display the title and body that you set in the content
object.