2

I tried to repeat a notification with UNCalendarNotificationTrigger(dateMatching:, repeats:), but this method can only repeat at a certain time.

I also tried UNTimeIntervalNotificationTrigger(timeInterval:, repeats:) and repeated a notification by a time interval, but this method could not set the start time of push notification.

And these two methods seems that there is no place to set a time for end push notifications.

I want to start with a special time and repeat notifications at regular intervals. What should I do?

K.B.
  • 291
  • 2
  • 13

1 Answers1

2

Rather than using the repeats parameter you could loop from your start time to end time scheduling individual notifications.

let notifIDPrefix = "mynotif"
let notifCategory = "com.mydomain.mynotif" // this should have been registered with UNUserNotificationCenter

func scheduleNotifs(from startDate: Date, to endDate: Date, with interval: TimeInterval) {
    var curDate = startDate
    var count: Int = 0
    while curDate.compare(endDate) != .orderedDescending {
        scheduleNotif(with: "\(notifIDPrefix)_\(count)", date: curDate)
        curDate = curDate.addingTimeInterval(interval)
        count += 1
    }
}

private func scheduleNotif(with identifier: String, date: Date) {

    let content = UNMutableNotificationContent()
    content.title = "My Title"
    content.body = " "
    content.categoryIdentifier = notifCategory
    content.sound = UNNotificationSound.default()

    let triggerTime = Calendar.current.dateComponents([.year, .day, .hour, .minute, .second], from: date)
    let trigger = UNCalendarNotificationTrigger(dateMatching: triggerTime, repeats: false)
    let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)

    let center = UNUserNotificationCenter.current()
    center.add(request) { (error : Error?) in
        if let theError = error {
            print(theError.localizedDescription)
        }
    }
}

The following would schedule 3 notifications (1, 2, and 3 minutes from now).

    let startDate = Date().addingTimeInterval(60)
    let endDate = startDate.addingTimeInterval(60 * 2)
    let interval: TimeInterval = 60
    scheduleNotifs(from: startDate, to: endDate, with: interval)
Devin Pitcher
  • 2,562
  • 1
  • 18
  • 11
  • Thanks, however, the iPhone schedules up to 64 notifications. If I set the end time for a long time from start time, the interval is very short, will this cause some notifications can't push? – K.B. Jun 14 '18 at 13:50
  • Hmm, good point. Not ideal, but you might reschedule all the notifications whenever your app enters the foreground. Obviously you'll hit the end of that 64 eventually if they never run the app. You could schedule a background task to run periodically to keep your next 64 (or less) scheduled https://developer.apple.com/library/archive/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html. I think you could specify the "fetch" mode in "UIBackgroundModes". Like I said, not ideal. I think the alternative is remote push notifications. – Devin Pitcher Jun 14 '18 at 14:07
  • Any solution to this yet ? I do not want to limit my notifications to 64 - but rather use the repeat-function instead. However, as mentioned, I need to be able to set a particular start date and from then on do the repetition of notifications at regular interval. – iKK May 31 '20 at 12:17