I am using the newer notification APIs introduced in iOS 10 and working in Swift. When I create a local notification, it shows up properly, but I am trying to remove it in my app when a relevant action occurs (so that the user doesn't have to clear it manually). But the delivered notification seems to persist.
I am scheduling a local notification like this:
let fireDate = Date() // some date, inputted as a method parameter
let calendar = Calendar.current
let dateComponents = calendar.dateComponents([.day, .month, .year, .hour, .minute], from: fireDate)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let notificationContent = UNMutableNotificationContent()
notificationContent.title = "some title"
notificationContent.body = " some message"
notificationContent.userInfo["custom key"] = "my custom key"
notificationContent.badge = 1
var components = DateComponents()
components.day = 10
notificationContent.userInfo["endDate"] = calendar.date(byAdding: components, to: fireDate) // haven't verified if this even works, but saw this as a way to set notifications to expire automatically, including for completeness
let request = UNNotificationRequest(identifier: identifier, content: notificationContent, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if let error = error {
print(error)
}
}
When an action in my app occurs such that the notification is no longer relevant, I want to remove it from the notification center, or prevent it from firing if it hasn't already:
func clearNotification(withIdentifier: String) {
let identifiers = [identifier]
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers)
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: identifiers)
UIApplication.shared.applicationIconBadgeNumber = UIApplication.shared.applicationIconBadgeNumber - 1
}
I've checked to make sure the identifier matches properly. If the notification hasn't fired yet, it doesn't. But if the notification has fired, it will remain in the notification center seemingly indefinitely. The app badge icon does seem to work as expected, but I don't really understand what removeDeliveredNotifications
is for if it's not for removing notifications from the notification center.
Note: I have tested it on the simulator as well as an actual iPhone (iOS 11.2.6).
It seems to be specifically related to UNCalendarNotificationTrigger
because if I switch to using a UNTimeIntervalNotificationTrigger
everything seems to work just fine. The notification based on a time interval trigger disappears when I call removeAllDeliveredNotifications
and it shows up in getDeliveredNotifications
. I can't understand why UNCalendarNotificationTrigger
would behave differently (I have read in the documentation that location-based triggers are a bit different, but not calendar triggers). I suspect this is a bug in iOS.
Edit: not a duplicate of this other SO question since that quesiton doesn't discuss this issue at all. Might be considered a duplicate of this one but that one doesn't have any answers.