Can we avoid displaying push notification

304 Views Asked by At

There is a scenario where I need to check if a user should receive push notification based on his location. I am aware that UNNotificationServiceExtension helps us intercept the notification and contributes in building push content. But is it possible for this class or for that matter from anywhere else that we don't display the push notification by checking the user's location ?

It doesn't mean I want to do this every time. But only when my condition is not met. In the notification content, I am getting user's last traced location which I'll use to compare with user's current location.
I went through this but didn't get a convincing answer.
Note : I am not looking for silent push notification solution.

1

There are 1 best solutions below

7
On

Using UNUserNotificationCenter you can cancel a single notification by id.

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [SomeID])

You'll have to implement the UNUserNotificationCenterDelegate and implement userNotificationCenter(_:willPresent)

Something like:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let identifier = notification.request.identifier
        let content = notification.request.content
        //your logic here
    }