I am using two date pickers to get start time and end time, but this will only trigger on the current date , but I want to trigger it daily.Will that be possible to do.
func scheduleNotifs(from startDate: Date, to endDate: Date, with interval: TimeInterval ) {
var curDate = startDate
var count: Int = 0
while curDate.compare(endDate) != .orderedDescending {
scheduleNotif(with: "\(notifIDPrefix)_\(count)", date: curDate)
curDate = curDate.addingTimeInterval(interval * 60 * 60)
count += 1
}
}
public func scheduleNotif(with identifier: String, date: Date) {
self.getUser()
let content = UNMutableNotificationContent()
content.title = "\(username),Want to be more healthy and fresh?"
content.body = "Water/Tea time"
content.categoryIdentifier = notifCategory
content.sound = UNNotificationSound.default
let notification = UILocalNotification()
notification.repeatInterval = NSCalendar.Unit.day
let triggerTime = Calendar.current.dateComponents([.hour, .minute, .second], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerTime, repeats: true)
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request) { (error : Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
}