Repeating local notifications for specific days of week (Swift 3 IOS 10)

Viewed 13829

I am trying to schedule local notifications for specific days of week (e.g. Monday, Wednesday and etc) and then repeat them weekly. This is how the screen for setting notifications looks:

enter image description here

User can select time for the notification and repeating days.

My method for scheduling single non repeating notification looks like this:

static func scheduleNotification(reminder: Reminder) {
    // Setup notification content.
    let content = UNMutableNotificationContent()
    
    //content.title = NSString.localizedUserNotificationString(forKey: "Reminder", arguments: nil)
    content.body = NSString.localizedUserNotificationString(forKey: reminder.reminderMessage, arguments: nil)
    content.sound = UNNotificationSound.default()
    
    
    // Configure the triger for specified time.
    // 
    let dateComponentes = reminder.dateComponents
    // TODO: Configure repeating alarm
    
    // For the testing purposes we will not repeat the reminder
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponentes, repeats: false)
    
    // Create the request object.
    let request = UNNotificationRequest(identifier: "\(reminder.reminderId)", content: content, trigger: trigger)
    
    // Schedule the request.
    let notificationCenter = UNUserNotificationCenter.current()
    notificationCenter.add(request) { (error: Error?) in
        if let theError = error {
            print(theError.localizedDescription)
        }
    }
}

The date components are extracted from UIDatePicker widget and stored in reminder class:

let date = reminderTimeDatePicker.date.addingTimeInterval(60 * 60 * 24 * 7)
let components = Calendar.current.dateComponents([.weekday, .hour, .minute], from: date)
...
reminder.dateComponents = components

I have an array selectedDays[Int] (as a property of reminder class) to hold info on days of week on which the notification should fire.

How can I schedule notification on specific day of week and how to repeat it weekly?

Even a single comment will be helpful, and thank you in advance.

2 Answers
Related