0

I'm facing the following issue. I have an app that request location in "always" mode that for some reason goes to sleep when the screen is off. It also asks for accelerometer data. All these are running inside a long running Task.

I thought that "location always" would keep the app to receive location data and thus maintains the app running.

The second problem is that the app should play a sound under certain circumstances while the screen is off, to notify the user to unlock it because he must interact with the app.

My info.plist contains the following:

<key>UIBackgroundModes</key>
<array>
    <string>bluetooth-central</string>
    <string>remote-notification</string>
    <string>processing</string>
    <string>location</string>
</array>

But AFAIK, it does not help achieve what I want.

Thanks for your help.

  • Location always will deliver location updates at a rate of about one per second as long as you have enabled background location updates, and set accuracy to "best" in order to activate the gps. I order to play a sound you should ask for local notification permission and just post a local notification with a sound. – Paulw11 Jan 30 '23 at 09:33
  • @Paulw11 AllowBackgroundLocationsUpdate = true; DesiredAccuracy = 1; –  Jan 30 '23 at 09:37
  • [this thread](https://developer.apple.com/forums/thread/69152) has some tips. It starts 6 years ago with outdated info; scroll down to the end and look at the last few entries. Note the mention of `pausesLocationUpdatesAutomatically`, and of ALSO using geofencing to wake up if necessary. Also note earlier the possibility of your app getting suspended or killed, then re-awakened: make sure you are re-establishing the location updates in these cases. (I don't know the details; I'm just reading online suggestions.) – ToolmakerSteve Jan 30 '23 at 20:57

1 Answers1

1

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.

Leo
  • 51
  • 2
  • Local notification with sound does not work. –  Feb 02 '23 at 09:41
  • Playing a sound with a notification does not work. I should schedule the sound to play at a specific time as explained [here](https://stackoverflow.com/a/68736750/15186). This works like a charm. –  Feb 02 '23 at 09:46