Why are my daily repeating local notifications not working after a few days?

Viewed 71

I have a reminder function in my app which is basically identical to the alarms in the native Clock app. For some reason, daily alarms work for the first few days then stop working entirely and I can't figure out why.

Scheduling:

  for day in self.days {
            //days is an array of weekdays 1-7

            let content = UNMutableNotificationContent()
            content.title = "Practice \(profile.name)"
            content.categoryIdentifier = "reminder"
            content.sound = UNNotificationSound.default
            
            var dateComponents = DateComponents()
            dateComponents.weekday = day
            dateComponents.hour = Calendar.current.component(.hour, from: self.date)
            dateComponents.minute = Calendar.current.component(.minute, from: self.date)
            dateComponents.second = 0
            let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
            
            //id(for: day) gives a unique id
            let request = UNNotificationRequest(identifier: id(for: day), content: content, trigger: trigger)
            center.add(request) { (error) in
                //there's never an error here
                print(error)
            }
        }

The notification fires perfectly for the first few days then mysteriously stops. Before it stops working, printing all pending notifications shows the notifications as expected. Once it mysteriously stops, printing all pending notifications shows that the notifications are no longer even pending, they're just gone.

How do I fix this, or even debug this?

0 Answers
Related