4

I'm fairly new to swift and am trying to call multiple functions that request a local notification inside a UISwitch IBAction. I want to send a notification on a certain date - each quarter of the year on months 4, 7, 10, 1. Only the fourth quarter function is getting called. How can I get all four functions to be called? Here is my code:

// UISwitch for quarterly notifications

@IBAction func quarterlyFrequencyTapped(_ sender: UISwitch) {

if quarterlyFrequency.isOn == true {

    firstQuarter(); secondQuarter(); thirdQuarter(); fourthQuarter()

    print("quarterly frequency is \(quarterlyFrequency.isOn)")

} else {

    removeQuarterlyNotification()

    print("quaterly frequency is \(monthlyFrequency.isOn)")

       }

}

// Functions for all four quarters

func firstQuarter() {
    let firstQuarterContent = UNMutableNotificationContent()
    firstQuarterContent.title = "First Quarter"
    firstQuarterContent.subtitle = "Some string"
    firstQuarterContent.body = "Some other string"

    var firstQuarterDate = DateComponents()
    firstQuarterDate.month = 3
    firstQuarterDate.day = 11
    firstQuarterDate.hour = 19
    firstQuarterDate.minute = 20

    let firstQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: firstQuarterDate, repeats: true)
    let firstQuarterRequestIdentifier = "Quarterly"
    let firstQuarterRequest = UNNotificationRequest(identifier: firstQuarterRequestIdentifier, content: firstQuarterContent, trigger: firstQuarterTrigger)
    UNUserNotificationCenter.current().add(firstQuarterRequest, withCompletionHandler: nil)
}

func secondQuarter() {
    let secondQuarterContent = UNMutableNotificationContent()
    secondQuarterContent.title = "Second Quarter"
    secondQuarterContent.subtitle = "Some string"
    secondQuarterContent.body = "Some other string"

    var secondQuarterDate = DateComponents()
    secondQuarterDate.month = 3
    secondQuarterDate.day = 11
    secondQuarterDate.hour = 19
    secondQuarterDate.minute = 21

    let secondQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: secondQuarterDate, repeats: true)
    let secondQuarterRequestIdentifier = "Quarterly"
    let secondQuarterRequest = UNNotificationRequest(identifier: secondQuarterRequestIdentifier, content: secondQuarterContent, trigger: secondQuarterTrigger)
    UNUserNotificationCenter.current().add(secondQuarterRequest, withCompletionHandler: nil)
}

func thirdQuarter() {
    let thirdQuarterContent = UNMutableNotificationContent()
    thirdQuarterContent.title = "Third Quarter"
    thirdQuarterContent.subtitle = "Some string"
    thirdQuarterContent.body = "Some other string"

    var thirdQuarterDate = DateComponents()
    thirdQuarterDate.month = 3
    thirdQuarterDate.day = 11
    thirdQuarterDate.hour = 19
    thirdQuarterDate.minute = 22

    let thirdQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: thirdQuarterDate, repeats: true)
    let thirdQuarterRequestIdentifier = "Quarterly"
    let thirdQuarterRequest = UNNotificationRequest(identifier: thirdQuarterRequestIdentifier, content: thirdQuarterContent, trigger: thirdQuarterTrigger)
    UNUserNotificationCenter.current().add(thirdQuarterRequest, withCompletionHandler: nil)
}

func fourthQuarter() {
    let fourthQuarterContent = UNMutableNotificationContent()
    fourthQuarterContent.title = "Fourth Quarter"
    fourthQuarterContent.subtitle = "Some string"
    fourthQuarterContent.body = "Some other string"

    var fourthQuarterDate = DateComponents()
    fourthQuarterDate.month = 3
    fourthQuarterDate.day = 11
    fourthQuarterDate.hour = 19
    fourthQuarterDate.minute = 23

    let fourthQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: fourthQuarterDate, repeats: true)
    //let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
    let fourthQuarterRequestIdentifier = "Quarterly"
    let fourthQuarterRequest = UNNotificationRequest(identifier: fourthQuarterRequestIdentifier, content: fourthQuarterContent, trigger: fourthQuarterTrigger)
    UNUserNotificationCenter.current().add(fourthQuarterRequest, withCompletionHandler: nil)
}
mfaani
  • 33,269
  • 19
  • 164
  • 293
nsd32
  • 63
  • 6
  • How do you know only the fourth is being called? There's nothing wrong with this block of code. – TheValyreanGroup Mar 12 '17 at 02:10
  • this `firstQuarter(); secondQuarter(); thirdQuarter(); fourthQuarter()` executes all 4...nothing wrong with. Perhaps your functions work incorrectly themselves – mfaani Mar 12 '17 at 02:10
  • To test this, I'm setting up each quarter's function one minute apart and run the simulator. Only the fourth notification runs in the simulator. – nsd32 Mar 12 '17 at 02:12
  • No. All 4 functions are being called with that line of code. You have something else wrong. – TheValyreanGroup Mar 12 '17 at 02:13

2 Answers2

6

I think you're on +iOS10 and you're using UserNotifications framework right?

Very likely your identifiers have the same and each are UPDATING the previous notification.

The reason identifier exists is to assist you with updated a previous notification that has the same identifier. For example you send a notification to update the score from 0-0 to 0-1. And instead of having 2 notifications on the screen you now have only one.

Your fix is to use different identifiers for each.

For more see this moment for the WWDC video

UPDATE after your edit: just as I said...all your notifications have "Quarterly" as their identifier. Give them each a separate name.

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • @nsd32 since you're new here. Once you get an answer you like, it's recommended to 1. upvote it 2. click on the check to [mark it as accepted answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – mfaani Mar 12 '17 at 02:44
  • 1
    thanks for the tip. I just clicked on the checkmark, however since my reputation is still low, the upvote does not show but apparently it still gets recorded somewhere. Thanks again for all the help. – nsd32 Mar 12 '17 at 02:47
1

Just a little help

You can just add UUID().uuidString for identifier.

Kudos Honey

zdravko zdravkin
  • 2,090
  • 19
  • 21