0

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.

Nitish
  • 13,845
  • 28
  • 135
  • 263

1 Answers1

2

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
    }
ahbou
  • 4,710
  • 23
  • 36
  • What might be the identifier for a push notification ? – Nitish Sep 13 '18 at 09:22
  • Right. But what would be the identifier (parameter) I need to pass ? From where would I get that ? – Nitish Sep 13 '18 at 09:29
  • can you add the code you use to build and present your notifications? – ahbou Sep 13 '18 at 09:31
  • This is the payload : { aps = { alert = "This is what your message will look like! Type in your message in the text area and get a preview right here"; badge = 1; "mutable-content" = 1; sound = default; }; "attachment-url" = ""; deeplinkurl = ""; "message_id" = 1793; } – Nitish Sep 13 '18 at 09:48
  • Ok but where do you do this -> "In the notification content, I am getting user's last traced location which I'll use to compare with user's current location" – ahbou Sep 13 '18 at 10:11
  • This is the proposed change. Currently being done by the web dev. – Nitish Sep 13 '18 at 10:16
  • I posted a sample implementation (in AppDelegate). Basically you have 2 options: Get the user location from the notification content or by calling your locationManager and doing your checks. – ahbou Sep 13 '18 at 10:22