Yes, this is possible: in your AppDelegate didFinishLaunchingWithOptions,
check if launched from notification (received APN while in background) and then start location services. When
you receive a location, send that to the backend and then shut down location services. Bear in mind that in
general there will be some delay before the system returns a location but this should be doable in the
amount of time that you have before the app sleeps again. There are some ways to get additional background
processing time if need be.
// untested code, ignores all of the boilerplate Location Services Authorization code and request user permission to Location services
var locationManager: CLLocationManager?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Check if launched from notification (received APN while in background)
if let notification = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: AnyObject] {
let aps = notification["aps"] as! [String: AnyObject]
print("received background apn, aps: \(aps)")
locationManager = CLLocationManager()
locationManager!.delegate = self
locationManager!.startUpdatingLocation() // this is what starts the location services query
}
...
return true
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
// here is where you would send location to your backend
// shut down location services:
locationManager?.stopUpdatingLocation()
locationManager?.delegate = nil
locationManager = nil
}