0

Using iOS13.4, XCode11.4, Swift5.2,

How can you set up a Local Notification that will trigger on every workday (i.e. Monday through Friday, but not Saturday and Sunday) ??

I tried different Calendar-Component setups but have not been successful for the workday-problem.

Here is what I've done so far...

If the alarm needs to be on a particular Date:

Calendar.current.dateComponents([.day, .month, .year, .hour, .minute, .second], from: notificationDate)

If the alarm needs to repeat daily:

Calendar.current.dateComponents([.hour, .minute], from: notificationDate)

If the alarm needs to repeat on a particular weekday:

Calendar.current.dateComponents([.weekday, .hour, .minute], from: notificationDate)

etc.

But how do you set it up for several weekdays (such as every workday Monday through Friday) ??

Here is my code for the precise notificationDate example:

import UIKit

class MyViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let content = UNMutableNotificationContent()
        content.title = "My Notification Title"
        content.categoryIdentifier = UUID().uuidString
        var notificationIdentifier = "23224-23134-234234"
        var notificationDate = Date(timeInterval: 30, since: Date())
        let notificationTriggerKind = Calendar.current.dateComponents([.day, .month, .year, .hour, .minute, .second], from: notificationDate)
        let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: notificationTriggerKind, repeats: false)
        let notificationRequest = UNNotificationRequest(identifier: notificationIdentifier, content: content, trigger: notificationTrigger)
        let notificationCenter = UNUserNotificationCenter.current()
        notificationCenter.delegate = self
        notificationCenter.add(notificationRequest) {(error) in if let error = error { print("error: \(error)") } }
    }
}

extension MyViewController: UNUserNotificationCenterDelegate {

    // kicks in when App is running in Foreground (without user interaction)
    func userNotificationCenter(_ center: UNUserNotificationCenter,
            willPresent notification: UNNotification,
            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        // ...do your custom code...
        completionHandler([.alert, .sound])
    }

    // kicks in when App is running in Foreground or Background
    // (AND App is open) AND user interacts with notification
    func userNotificationCenter(_ center: UNUserNotificationCenter,
        didReceive response: UNNotificationResponse, withCompletionHandler
        completionHandler: @escaping () -> Void) {

        // ... do your custom code ....
        return completionHandler()
    }
}
iKK
  • 6,394
  • 10
  • 58
  • 131

1 Answers1

0

You were on a good way.

To make that to specific days you need to schedule one notification for each day.

Check this example:

  • Create and extension of date to get hour, minutes and seconds from specific date
        extension Date {
        public func getDateComponents() -> DateComponents {
               let dateComponents = Calendar.current.dateComponents([ .hour, .minute, .second], from: self)

               return dateComponents
           }
    }
  • Or create a date components directly:
    var dateComponents = DateComponents()
    dateComponents.hour = 9
    dateComponents.minutes = 10
    dateComponents.weekDay = 1
  • Then check take days from Monday(2) to Friday(6) And iterate an array of work days to create a specific notification for each day and repeat.
    let workDays = 2...6
            for day in workDays {
                var dateComponents = date.getDateComponents()
                dateComponents.weekday = day

                let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: repeats)

                let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)

                self.notificationCenter.add(request) { (error) in
                    if let error = error {
                    }
                }
            }
NSViking
  • 53
  • 6