Cancel a repeating UNNotification for today only

379 views Asked by At

I am scheduling daily repeating notifications by setting only hour, minutes and seconds:

func addNotification(fireDate: Date)
{
    let notificationCenter = UNUserNotificationCenter.current()
    let content = UNMutableNotificationContent()
    content.title = "Some title"
    content.body = "Some message"
    content.sound = .default
    content.categoryIdentifier = "MyCategory"
        
    let calendar = Calendar(identifier: .gregorian)
    let triggerDate = calendar.dateComponents([.hour, .minute, .second], from: fireDate)
    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: true)

    let requestID = UUID().uuidString
    let notificationRequest = UNNotificationRequest(identifier: requestID, content: content, trigger: trigger)
    notificationCenter.add(notificationRequest) { error in
            
        if let error = error
        {
            print(error.localizedDescription)
        }
    }
}

Now I would like to be able to cancel all notifications for current day, so I could receive notifications starting with tomorrow. I tried to subclass UNCalendarNotificationTrigger in order to change nextTriggerDate, but there is an error when adding a notification using my custom trigger class. Cannot find a way to achieve this, if I cancel a scheduled notification at certain time, I will not receive that notification anymore. Any ideas are appreciated.

0

There are 0 answers