Below is an example of LocationService usage with background mode.
public class LocationManager
{
protected CLLocationManager locMgr;
public event EventHandler<LocationUpdatedEventArgs> LocationUpdated = delegate { };
public LocationManager()
{
this.locMgr = new CLLocationManager
{
// This mode is resistant to applications being killed in the background
PausesLocationUpdatesAutomatically = false
};
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
locMgr.RequestAlwaysAuthorization();
}
if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
{
locMgr.AllowsBackgroundLocationUpdates = true;
}
}
public CLLocationManager LocMgr
{
get { return this.locMgr; }
}
public void StartLocationUpdates()
{
if (CLLocationManager.LocationServicesEnabled)
{
LocMgr.DesiredAccuracy = 1;
LocMgr.LocationsUpdated += (object sender, CLLocationsUpdatedEventArgs e) =>
{
LocationUpdated(this, new LocationUpdatedEventArgs(e.Locations[e.Locations.Length - 1]));
};
LocMgr.StartUpdatingLocation();
}
}
}
When an application monitors the location service in background mode, you can try local notification to post with a sound
// Trigger by position
@IBAction func locationInterval(_ sender: Any) {
let content = UNMutableNotificationContent()
content.title = "xx"
content.body = "xx"
let coordinate = CLLocationCoordinate2D(latitude: 31.29065118, longitude: 118.3623587)
let region = CLCircularRegion(center: coordinate, radius: 500, identifier: "center")
region.notifyOnEntry = true
region.notifyOnExit = false
let trigger = UNLocationNotificationTrigger(region: region, repeats: true)
let requestIdentifier = "com.abc.testUserNotifications"
let request = UNNotificationRequest(identifier: requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
Hope it helps.