LocationService is a .NET MAUI sample app (targeting .NET 10) that demonstrates how to obtain periodic GPS location updates and keep delivering them while the app runs in the foreground and — with platform support and the proper permissions — in the background.
The sample was originally developed to collect and broadcast location updates for mobile apps and includes helpers for permission management, a simple background loop that fetches location periodically, and lightweight messaging to deliver updates to the app UI.
- Periodic location updates (configurable interval)
- Permission consent helpers for iOS
- Background processing pattern for polling GPS and sending updates
- Small, testable core with platform-specific implementations when required
src/BackgroundServices/LocationUpdates.cs— the periodic worker that requests the device location and publishes updates viaWeakReferenceMessenger(LocationUpdateandLocationErrorMessage). The default delay is currently 5000 ms and theGeolocationRequestusesGeolocationAccuracy.Highwith a 1s timeout; adjust these values to change frequency/accuracy.src/Platforms/iOS/LocationManager.csandsrc/Platforms/iOS/PermissionConsent.cs— iOS-specific location manager wrapper and a permission-consent helper that requests "always" authorization and enablesAllowsBackgroundLocationUpdateswhen available.src/Platforms/Android/PermissionConsent.cs— the Android counterpart that is responsible for requesting runtime location permissions and handling consent flows on Android devices.src/Platforms/Android/Services/AndroidLocationService.csandsrc/Platforms/iOS/Services/iOsLocationService.cs— platform-specific background service implementations. These files contain the OS integration required to run theLocationUpdatesworker in the background (for example, Android foreground service / worker integration and the iOSBeginBackgroundTaskwrapper used iniOsLocationService).- Messaging models in
src/Messages/(for errors and updates) andsrc/Models/LocationModel.cshold simple location payloads.
- iOS (uses CoreLocation/CLLocationManager; requests Always authorization when appropriate)
- Android (via .NET MAUI Geolocation APIs; requires proper manifest permissions and battery-optimisation tweaks on some devices)
- Mac Catalyst and Windows are included in the project multi-target but platform background semantics differ — consult platform docs.
- This sample requests Always location access in
PermissionConsentand setsAllowsBackgroundLocationUpdatesinLocationManagerwhen the OS supports it. - Update your
Info.plistwith the appropriate usage descriptions (user-facing text):NSLocationWhenInUseUsageDescriptionNSLocationAlwaysAndWhenInUseUsageDescription- Optionally
NSLocationAlwaysUsageDescription(older iOS versions)
- Add the
locationbackground mode inUIBackgroundModesif your app needs to continue collecting locations while suspended.
- Add runtime permissions in
AndroidManifest.xmland request them at runtime for Android 6+:ACCESS_FINE_LOCATIONACCESS_COARSE_LOCATIONACCESS_BACKGROUND_LOCATION(required to access location while the app is in the background on modern Android versions)
- Some manufacturers aggressively stop background work to save battery — you may need to instruct users to exclude the app from battery optimizations or whitelist it.
- Consider using a foreground service (with a notification) if you need long-running background location updates on Android.
- Polling frequency: edit
Task.Delay(5000, token)insrc/BackgroundServices/LocationUpdates.csto change how often the sample attempts to fetch location. - Accuracy/timeout: tune the
GeolocationRequestparameters in the same file to balance accuracy and battery usage. - Preferences: the project contains a small preferences service (
src/Utils/PreferencesService.cs) and aSERVICE_STATUS_KEYconstant used to persist state.
- Visual Studio with .NET MAUI workload installed (support for .NET 10 in the product matching the SDK target)
- Or install .NET SDK and MAUI workloads:
dotnet workload install maui(use a terminal that supports your platform)
- Open the solution in Visual Studio and deploy to a simulator or device for the target platform.
- From the command line you can build or test the projects:
dotnet build— builds all projectsdotnet test tests/LocationService.Tests— run the test suite
Notes: Some platforms (iOS/tvOS/macCatalyst/Android) require deployment to a device (not all simulators provide background behaviors or accurate hardware location). For full background behavior test on a real device.
The solution includes a tests project at tests/LocationService.Tests. Execute dotnet test against that project to validate the test suite. Unit tests should be kept platform-agnostic; platform-specific code can be integration-tested on devices.
Contributions, bug reports and suggestions are welcome. If you plan to add features, try to keep platform-specific code isolated under src/Platforms/* and common logic under src/ so unit tests can run without device dependencies.
This repository includes Apache-2.0 license headers in source files. Confirm and add a repository-level LICENSE file if you intend to publish.
- .NET MAUI documentation: https://learn.microsoft.com/dotnet/maui/
- Apple Core Location guide: https://developer.apple.com/documentation/corelocation
- Android location permissions and background location: https://developer.android.com/training/location
