Search code examples
iosswiftunnotificationtrigger

Scheduling notification with Swift 3 for an array of dates


I have an array containing my dates. I want to schedule notifications for those days at 6.30am.

I followed the appcoda tutorial which helps scheduling the notification upon input from a datepicker which is great, but I am a bit uncertain on how to call my function to schedule the notification for only the given days.

So my question is how and where to call the function?

  • the days are consecutive days
  • can I give the function a start date and repeat it with the number of items in the array?

Below is my function:

    func scheduleNotification(at date: Date) {
        let calendar = Calendar(identifier: .gregorian)
        let components = calendar.dateComponents(in: .current, from: date)
        let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: 6, minute: 30)
        let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)

        let content = UNMutableNotificationContent()
        content.title = "Advent Calendar"
        content.body = "Just a reminder to open your present!"
        content.sound = UNNotificationSound.default()

        let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
 UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
        UNUserNotificationCenter.current().add(request) {(error) in
            if let error = error {
                print("Uh oh! We had an error: \(error)")
            }
        }
    }

Solution

  • Right, so after consulting with a developer @gklka I have decided to use a simple for loop that repeats 24 times ) and it passes the index to the function's day property, where I preconfigured the hour, minute, year and month like so:

    func scheduleNotification(day: Int) {
    
        var date = DateComponents()
        date.year = 2016
        date.month = 11
        date.day = day
        date.hour = 6
        date.minute = 30
    
        let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: false)
    }
    

    and the for loop:

    for index in 1...24 {
            scheduleNotification(day: index)
        }
    

    Since I had everything set up int AppDelegate I call the function in didFinishLaunchingWithOptions

    Update on the 1st of Dec.

    So I left everything as is but no notification has occurred in the morning. 😔. I looked into my code to figure out why. There were 2 issues.

    1. I had a line of code within my function that would delete any previous notification set up while iterating through my loop, so I had the below line of code commented out, but things still did not work as expected. UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

    2. I found that I have scheduled my notification with the same requestIdentifier, which basically left me with only 1 notification for the last day. I simply added the index at the end of my custom ID variable with string interpolation, like so: let requestId = "textNotification\(day)"