How do I set an NSCalendarUnitMinute repeatInterval on iOS 10 UserNotifications?

Although it appears that the old notifications framework had better support regarding this, it’s only until, we realise that the new one is better!!

I had similar problems, and below is a sample of how the old notifications can go hand in hand with the new ones.

To be on a safer side, this is Swift2.3.

// Retrieve interval to be used for repeating the notification
    private func retrieveRepeatInterval(repeatTemp: RepeatInterval) {
        switch repeatTemp {
        case .Never:
            if #available(iOS 10.0, *) {
                toDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute, .Day, .Month, .Year], fromDate: timeTemp!)
                toDateComponents.second = 0
                repeatNotification = false
            } else {
                repeatIntervalTemp = nil
            }
        case .EveryMinute:
            if #available(iOS 10.0, *) {
                toDateComponents.second = 0
                repeatNotification = true
            } else {
                repeatIntervalTemp = .Minute
            }
        case .EveryHour:
            if #available(iOS 10.0, *) {
                toDateComponents = NSCalendar.currentCalendar().components([.Minute], fromDate: timeTemp!)
                toDateComponents.second = 0
                repeatNotification = true
            } else {
                repeatIntervalTemp = .Hour
            }
        case .EveryDay:
            if #available(iOS 10.0, *) {
                toDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute], fromDate: timeTemp!)
                toDateComponents.second = 0
                repeatNotification = true
            } else {
                repeatIntervalTemp = .Day
            }
        case .EveryWeek:
            if #available(iOS 10.0, *) {
                toDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute, .Weekday], fromDate: timeTemp!)
                toDateComponents.second = 0
                repeatNotification = true
            } else {
                repeatIntervalTemp = .WeekOfYear
            }
        case .EveryMonth:
            if #available(iOS 10.0, *) {
                toDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute, .Day], fromDate: timeTemp!)
                toDateComponents.second = 0
                repeatNotification = true
            } else {
                repeatIntervalTemp = .Month
            }
        case .EveryYear:
            if #available(iOS 10.0, *) {
                toDateComponents = NSCalendar.currentCalendar().components([.Hour, .Minute, .Day, .Month], fromDate: timeTemp!)
                toDateComponents.second = 0
                repeatNotification = true
            } else {
                repeatIntervalTemp = .Year
            }
        }
    }

Although this isn’t exhaustive, I’ve pretty much covered everything from a minute to a year.

And to be more detailed,

// RepeatInterval
enum RepeatInterval : String, CustomStringConvertible {
    case Never = "Never"
    case EveryMinute = "Every Minute"
    case EveryHour = "Every Hour"
    case EveryDay = "Every Day"
    case EveryWeek = "Every Week"
    case EveryMonth = "Every Month"
    case EveryYear = "Every Year"

    var description : String { return rawValue }

    static let allValues = [Never, EveryMinute, EveryHour, EveryDay, EveryWeek, EveryMonth, EveryYear]
}

var repeatTemp: RepeatInterval?

var repeatIntervalTemp: NSCalendarUnit?

var timeTemp: NSDate?

var toDateComponents = NSDateComponents()

As this works for me, it should probably for the rest of you. Please try and let me know if there is an issue.

And, for the exact solution to this question, I’d advise the below.

(Probably, Apple didn’t think about such a scenario, but it can be handled)

  1. Schedule a notification for 8:30 PM
  2. When the notification is triggered, remove it and schedule another notification with a different identifier, for the NSCalendarUnitMinute equivalent shown above.
  3. Voila, it works!! (Or did it not?)

Leave a Comment