I have a time picker and a textfield where user enter the time and title and I want to send a local notification at the time the user selected. My code work to send one notification but if I add another notification it overrides the first one and the first one does not get sent. I think it may be because of the identifier but don't know how to solve that.
Here is my code:
let calendar = Calendar.current
let datec = Date()
let hour = Calendar.current.component(.hour, from: StartTimePicker.date)
let minute = Calendar.current.component(.minute, from: StartTimePicker.date)
let year = calendar.component(.year, from: datec)
let month = calendar.component(.month, from: datec)
let day = calendar.component(.day, from: datec)
let components = DateComponents(year: year, month: month, day: day, hour: hour, minute: minute) // Set the date here when you want Notification
let date = calendar.date(from: components)
let comp2 = calendar.dateComponents([.year,.month,.day,.hour,.minute], from: date!)
let trigger = UNCalendarNotificationTrigger(dateMatching: comp2, repeats: true)
let content = UNMutableNotificationContent()
content.title = "Schedule Alert"
content.body = TitleTextField.text! + " from " + time
let request = UNNotificationRequest(
identifier: "identifier",
content: content,
trigger: trigger
)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
if error != nil {
//handle error
} else {
//notification set up successfully
}