0

My task is to set a period of time when the user receives notifications. So I know this period (10.00–11.00), interval (5 min) and can calculate the number of notifications (period/interval = 12). So the app adds 12 requests to UNUserNotificationCenter. In these conditions, everything works fine. But there is a problem:

An app can have only a limited number of scheduled notifications; the system keeps the soonest-firing 64 notifications (with automatically rescheduled notifications counting as a single notification) and discards the rest. If the user sets a longer period (10 hours) the number of scheduled notifications = 120 and they are not firing.

What can I do about the 64 notifications limit?

Maybe there's another way to schedule notifications in a specified period of time?

My code:

let calendar = Calendar.current
let period = calendar.dateComponents([.minute], from: self.startTime, to: self.endTime)

var intPeriod: Int {
    var intPeriod = period.minute!
        if intPeriod < 0 {
        intPeriod = 1440 + intPeriod
    }
    return intPeriod
}
let quantity = intPeriod / minutes

var increasingMinutes = 0

for _ in 0.. < quantity + 1 {
    var increasingTrigger = calendar.date(byAdding: .minute, value: increasingMinutes, to: self.startTime)
    let dc = calendar.dateComponents([.hour, .minute], from: increasingTrigger!)
    let trigger = UNCalendarNotificationTrigger(dateMatching: dc, repeats: true)
    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request) {
        (error) in
        print("Error \(String(describing: error))")
    }
    increasingMinutes += minutes
}

startTime, endTime - bindable vars from DatePicker

minutes - bindable from Stepper

kimcodes
  • 736
  • 1
  • 6
  • 16
  • Are you aware of [`UNTimeIntervalNotificationTrigger`](https://developer.apple.com/documentation/usernotifications/untimeintervalnotificationtrigger/1649777-init)? If you use that, you get **one** constantly repeating notification, until you programmatically cancel it. – Sweeper Oct 02 '20 at 13:24
  • However, as for _how_ to programmatically cancel it, it's an [unanswered question](https://stackoverflow.com/questions/49743772/cancel-notification-after-a-set-amount-of-time-or-repetitions). – Sweeper Oct 02 '20 at 13:25
  • Hi, Sweeper! Does your second message mean that UNTimeIntervalNotificationTrigger does not work in this situation? – Николай Надеев Oct 02 '20 at 17:46
  • You can add the `UNUserNotificationCenterDelegate` protocol and implement the `public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)` method to install a completion handler that is called when the notifications are presented. You can schedule a new notification as the old one expires. Repeat until all of your notifications have been presented. – DPrice Oct 03 '20 at 06:34
  • DPrice! I cann't understand your idea, too complicated for me right now. Maybe you have some sample that I can use? Thanks! – Николай Надеев Oct 03 '20 at 10:22

0 Answers0