1

I have properly set up location based local notifications. As of now the user receives a notification when they enter a defined zone (CLCircularRegion). Id like to delay that notification about 10 minutes after they enter the zone, could that be possible?

// SENDING WITH (UNLocationNotificationTrigger)

 let trigger = UNLocationNotificationTrigger(region: destRegion, repeats: false)
 let request = UNNotificationRequest(identifier: notificationInfo.notificationId,
                                        content: notification,
                                        trigger: trigger)
 UNUserNotificationCenter.current().add(request)

I also send notification locally for other reasons at different places in my app. This causes a delay for the elapsedSeconds.

// SENDING WITH (UNTimeIntervalNotificationTrigger)

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: elapsedSeconds, repeats: false)
let request = UNNotificationRequest(identifier: "cartabandon", content: localnotification, trigger: trigger)
UNUserNotificationCenter.current().add(request)

Maybe theres a way to combine the two? Or another option?

robotos
  • 194
  • 11
  • 2
    I think you would need to ask for and get "always" location permission so that you could set up your own region monitoring. Then when the user enters the region you can schedule a local notification for 10 minutes time. If you get a region exit before then, cancel the notification. – Paulw11 Nov 16 '22 at 22:19
  • Ahh okay now I see I can probably use locationManager(_:didEnterRegion:) as an alternative. However I am able to use UNLocationNotificationTrigger both when the app is terminated and .whenInUse permission is available (don't need .always). Might not be worth the 10 minutes if we need extra permission. And I will have to test how to app reacts when it is terminated. Thank you for pointing me in the right direction! – robotos Nov 16 '22 at 23:15
  • There is already some hysteresis built in to region monitoring; The notification won't trigger the instant that they cross the threshold, but it will probably trigger sooner than 10 minutes after they enter. – Paulw11 Nov 17 '22 at 00:15
  • Great to know I will leave it for now tbh I haven't tested on a physical device yet. On the simulator it triggers instantly regardless of app state. If I'm unhappy with the timing on the physical device I will consider the alternative. I appreciate the information! – robotos Nov 17 '22 at 04:12

0 Answers0