4

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)")
            }
        }
    }
Sarabjit Singh
  • 1,814
  • 1
  • 17
  • 28
Ben S
  • 558
  • 3
  • 18

1 Answers1

1

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)"

Ben S
  • 558
  • 3
  • 18
  • how does this new func scheduleNotification interact with the previous function in your question where you create the notification? Is your alert content the same for all alerts – tylerSF Feb 03 '17 at 14:30
  • @tylerSF, it creates all the 24 notifications at once, scheduled for each day. The app contains a new gift every day, therefore the notifications is the same, just to bring the information to the user's attention. – Ben S Feb 19 '17 at 12:18