I’m engaged on a medication reminder utility, the place person can add medication reminders for both each day, weekly or alternate days and choose a time for the reminder. It was a straight ahead implementation for each day and weekly however I’m struggling to discover a resolution for alternate days as Apple would not present any choice to move in frequency worth with Calendar Notification Set off. I’ve searched everywhere in the web however have not discovered any resolution. Up to now what I’ve executed is that if person choose alternate days for that reminder, I’m creating a number of notifications in a loop. However solely my first notification within the loop is getting fired and if change the system date and time to examine the subsequent notification, it did not work. I’m including my code right here, somebody please right me if I’m doing something incorrect.
func scheduleNotification(_ notification: Notification) {
let content material = UNMutableNotificationContent()
content material.physique = Constants.notificationDescription
content material.title = notification.title
content material.sound = .default
content material.userInfo = ["id":notification.id]
content material.categoryIdentifier = Constants.notificaitonCategoryIdentifier
// Notification Actions
let takenAction = UNNotificationAction(identifier: Constants.notiTakenActionIdentifier, title: Constants.notificationTakenText)
let notTakenAction = UNNotificationAction(identifier: Constants.notiNotTakenActionIdentifier, title: Constants.notificationNotTakenText)
let actionCategory = UNNotificationCategory(identifier: Constants.notificaitonCategoryIdentifier, actions: [takenAction, notTakenAction], intentIdentifiers: [])
UNUserNotificationCenter.present().setNotificationCategories([actionCategory])
var dateComponents = DateComponents()
if notification.frequency == .each day {
dateComponents.hour = notification.hour
dateComponents.minute = notification.min
let set off = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let nextTriggerDate = set off.nextTriggerDate()
let request = UNNotificationRequest(
identifier: notification.id,
content material: content material,
set off: set off)
UNUserNotificationCenter.present().add(request)
} else if notification.frequency == .weekly {
dateComponents.hour = notification.hour
dateComponents.minute = notification.min
dateComponents.weekday = notification.weekDay
let set off = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
let nextTriggerDate = set off.nextTriggerDate()
let request = UNNotificationRequest(
identifier: notification.id,
content material: content material,
set off: set off)
UNUserNotificationCenter.present().add(request)
} else if notification.frequency == .alternateDay {
let firstDate = notification.date
let firstTimeInterval = firstDate.timeIntervalSince1970
for i in stride(from: 0, to: 30, by: 2) {
let timeIntervalForTrigger = firstTimeInterval + Double((i * 86400))
let date = Date.init(timeIntervalSince1970: timeIntervalForTrigger)
let dateComponents = date.getDateComponents()
let set off = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let request = UNNotificationRequest(
identifier: notification.id + String(i),
content material: content material,
set off: set off)
UNUserNotificationCenter.present().add(request)
}
self.getPendingRequests()
}
}
Right here I’m utilizing stride so as to add 2 days in each iteration and once I examine the dates with print assertion all of the dates are printed and proper however the notification just isn’t arising apart from the primary one. Thanks prematurely.