3

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.

shim
  • 9,289
  • 12
  • 69
  • 108
  • did you try `removeAllDeliveredNotifications()` ? – BilalReffas Mar 16 '18 at 19:53
  • lol you caught me just as I was about to try it. – shim Mar 16 '18 at 19:53
  • No, the notifications are still there when I swipe down from the top. – shim Mar 16 '18 at 19:55
  • Can you try it on a real iDevice? – BilalReffas Mar 16 '18 at 19:57
  • Same thing on a real device. – shim Mar 16 '18 at 20:08
  • Please try to set `UIApplication.shared.applicationIconBadgeNumber = 0` – BilalReffas Mar 16 '18 at 20:17
  • Doesn't seem to make any difference. – shim Mar 16 '18 at 20:21
  • I also can't seem to get anything when printing the contents of `UNUserNotificationCenter.current().getDeliveredNotifications` (even if I remove my calls to `removeAllDeliveredNotifications`) – shim Mar 16 '18 at 20:25
  • I am trying it with a `UNTimeIntervalNotificationTrigger` instead of a calendar trigger, and I think it works — so maybe it's specific to the calendar trigger? – shim Mar 16 '18 at 20:41
  • I see that you've gotten around it, but I wonder, did you try `removeAllPendingNotificationRequests()`? – SaganRitual Mar 16 '18 at 22:45
  • It’s not a workaround. I need to use the calendar trigger. Remove all pending works fine. The scheduled notification doesn’t fire. But if it’s been delivered already it won’t disappear from the Notification Center. – shim Mar 16 '18 at 22:53
  • This sounds familiar: https://stackoverflow.com/questions/48177108/difference-bwteen-untimeintervalnotificationtrigger-and-uncalendarnotificationtr -- and this: https://stackoverflow.com/questions/38922892/how-can-i-remove-previously-delivered-notifications-when-a-new-notification-arri – SaganRitual Mar 17 '18 at 13:10
  • Possible duplicate of [How can I remove previously delivered notifications when a new notification arrives with UNUserNotificationCenterDelegate in iOS 10?](https://stackoverflow.com/questions/38922892/how-can-i-remove-previously-delivered-notifications-when-a-new-notification-arri) – SaganRitual Mar 17 '18 at 13:16
  • No solutions there either though. Also I don't see how that question is a duplicate. – shim Mar 18 '18 at 00:26
  • Also potentially relevant: https://openradar.appspot.com/36019616 – shim Mar 18 '18 at 18:21

0 Answers0