1

Is it possible to have a short evaluation process happen before a local notification is being presented?

Depending on the outcome of this evaluation, I would then cancel/remove a potentially unnecessary notification.

Bernd
  • 11,133
  • 11
  • 65
  • 98
  • Are your notifications time based ? – Abdul91 Jan 04 '18 at 13:04
  • Possible duplicate of [UserNotifications cancel, swift3](https://stackoverflow.com/questions/40562912/usernotifications-cancel-swift3) – Prashant Tukadiya Jan 04 '18 at 13:07
  • @PrashantTukadiya I do not think so, as that answer states How to delete a registered pending notification. This question is more about When. – Abdul91 Jan 04 '18 at 13:38
  • @Abdul91 it is all about removePendingNotificationRequests from the queue of notification. so it is related. If you have scheduled notification then it is in queue. if you don't want it remove it. there is no when – Prashant Tukadiya Jan 04 '18 at 13:39
  • Possible duplicate of [Delete a particular local notification](https://stackoverflow.com/questions/6340664/delete-a-particular-local-notification) – Cœur Mar 24 '19 at 15:29

1 Answers1

0

Yes it is. Note that mentioning:

Is it possible to have a short evaluation process happen before a local notification is being presented?

Means that the notification status is Pending.

So what you should do is to call removePendingNotificationRequests(withIdentifiers:) and passing the notification identifier(s) to it that you want it/them to be removed.

You could implement it like this:

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ["notificationID"])

and that should do the job.

Note that it takes identifiers as an array of strings, even if your need is to remove only one notification, you would need to pass an array containing one string.

Obviously, "notificationID" is the used identifier for your notification that you are using when you registering it (when creating the UNNotificationRequest):

// ...
let request = UNNotificationRequest(identifier: "notificationID", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • 1
    When are you calling removePendingNotificationRequests ? – Abdul91 Jan 04 '18 at 13:28
  • Thanks. That makes sense. But I guess this would require a background process to `removePendingNotificationRequests` without the app being active? – Bernd Jan 04 '18 at 13:45
  • @Abdul91 based on the OP request, it should be depends on "short evaluation process" validation... – Ahmad F Jan 04 '18 at 13:57
  • @Bernd you might be able to achieve it, checking the two methods of [UNUserNotificationCenterDelegate](https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate) hopefully would be useful to your case. – Ahmad F Jan 04 '18 at 14:01