Scheduling notifications in Swift using UNUserNotificationCenter allows you to send alerts, reminders, and other notifications to users. Here's how you can set up local notifications step by step.
import UserNotifications
// Request permission to display alerts and play sounds.
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { granted, error in
if granted {
print("Permission granted")
} else {
print("Permission denied")
}
}
// Create a new notification content
let content = UNMutableNotificationContent()
content.title = "Reminder"
content.body = "This is your scheduled notification."
content.sound = UNNotificationSound.default
// Configure a time-based trigger (e.g., 5 seconds from now)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
// Create the request for the notification
let request = UNNotificationRequest(identifier: UUID().uuidString, 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: \(error.localizedDescription)")
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?