I have following Problem: I want that the User can set an Interval (for example 1 Week, 2 Weeks, 3 Weeks, 4 Weeks and so on...). If this Interval elapses a notification should trigger and display something to the User.
This is my Code for scheduling a notification (Goal is my custom object):
func scheduleNewNotification(for goal : Goal, at trigger : DateComponents) {
let notificationContent = UNMutableNotificationContent()
notificationContent.title = NSLocalizedString("time", comment: "")
notificationContent.body = "\(goal.name) " + NSLocalizedString("notifyBody", comment: "")
notificationContent.sound = UNNotificationSound.default
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: trigger, repeats: true)
let request = UNNotificationRequest(identifier: goal.name, content: notificationContent, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
Every time when I want to schedule a notification I call this code:
func configureTrigger() -> DateComponents {
var date = DateComponents()
switch selectedInterval {
case .weekly:
// return configured date
break
case .twoWeeks:
// return configured date
break
// and so on ...
default:
break
}
return date
}
When the switch case got to a .weekly for example I want it to configure the DateComponents in a way where I only have to pass this to my scheduleNewNotification(), so that the user get a notification every selected interval.
Is this even possible with only a DateComponents?