I used the following code to trigger a daily reminder:
let triggerDate = calendar.date(from: calendarComponents)
let triggerDaily = Calendar.current.dateComponents([.hour, .minute, .second], from: triggerDate!)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let request = UNNotificationRequest(identifier: "daily-identifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
//UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [identifier])
UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in
if error != nil {
debugPrint("center.add(request, withCompletionHandler: { (error)")
} else {
debugPrint("no error?! at request added place")
}
})
The above code allows me to set a daily alarm with identifier daily-identifier
. Since I plan to set two daily alarm in my code, I used the above code and use another identifier second-daily-identifier
to trigger another alarm with different hour/minutes combination.
Problem I encountered: If I use the code
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
before adding the request, it will remove the alarm that set earlier (e.g., if i add "daily-identifier" alarm first, then when i add "second-daily-identifier" will remove the "daily-idenifier" alarm.
If i use the other method
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [identifier])
to specifically remove the one I am going to set, then it works well. However, I found that I can never stop it any more. e.g., if I have change the identifier to "third-identifier", and then the already set alarm will be in my phone forever. and I cannot remove them even I remove the app.
What have I done wrong?