Scheduled local notifications mysteriously disappearing

I have a feature in my app which is very similar to native Clock alarms, where users can set a time for a reminder and optional days of the week to repeat the reminder.

Example Case
User sets a reminder at 1:00 PM, repeating every day. I schedule 7 notifications, 1 for each weekday, each repeating. The user should get a notification every day at 1:00 PM unless they turn off the feature.

Problem
After successfully scheduling local notifications, and it working for the first day, the notifications stop working and printing pending notifications shows that they mysteriously got unscheduled.

What I know
  • I've heard of a 64 limit to notifications, but when testing with 7 notifications (1 reminder for each day of the week, repeating) it doesn't work either.

  • Each notification has an ID in the format:

Code Block
<my bundle id>.<UUIDstring>
  • It consistently works on the first day, then fails on the 2nd or 3rd day

  • The app is in production, the problem occurs in both development and production environments.


Code

Code Block swift
for day in self.days {
/*
days is an array of integers representing weekdays
*/
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:) generates a unique id using a UUID and the weekday.
*/
let request = UNNotificationRequest(identifier: id(for: day), content: content, trigger: trigger)
center.add(request) { (error) in
print(error) /*never an error here*/
}
}


The code seems to work exactly as intended; printing pending notifications shows 7 repeating notifications on different days as expected. However after a few days it stops working, and printing pending notifications shows no pending notifications.

I really need to get this resolved. Any ideas?



I have the exact same issue. Did you solve this problem?

@funju, yes I did but it was just me overlooking older code. Basically a long time ago in the app I had a simple timer notification and if the user cancels it I naively removed the pending notification by calling:

UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

Which unscheduled all notifications, including the ones that were "mysteriously" disappearing. It's unlikely but I'd suggest doing a simple search of your code base to make sure that isn't being called. So it was an easy fix by replacing that line with:

UNUserNotificationCenter.current().getPendingNotificationRequests { requests in
    let timerRequests = requests
        .filter({ $0.content.categoryIdentifier == self.timerNotificationCategoryIdentifier })
        .map({ $0.identifier })

    UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: timerRequests)
}

I had a similar issue - what I was doing was calling removeAllPendingNotificationRequests immediately before I scheduled the new ones, just to clean everything up before reseting. The problem with that is that this method does the actual work on a background thread, so if you immediately add new notifications it's possible that they are deleted by the deferred removal.

Scheduled local notifications mysteriously disappearing
 
 
Q