Ionic3中cordova-plugin-background-geolocation后台定位停止问题求助
Hey there! Let’s tackle that background geolocation issue you’re facing with the cordova-plugin-background-geolocation. I’ve debugged plenty of these cases, so here are the most critical fixes to get it running reliably in the background:
Permissions make or break background tracking—don’t skip this step:
- Android: You need three key permissions for Android 10+ (and newer):
ACCESS_FINE_LOCATION,ACCESS_COARSE_LOCATION, andACCESS_BACKGROUND_LOCATION. Add these to yourconfig.xml:
Don’t forget to request these dynamically at runtime—background permissions can’t be granted just via the manifest; users have to explicitly allow them.<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> - iOS: Update your
Info.plistwith clear permission descriptions for both foreground and background use:
Also, in Xcode, enable the Location updates option under your project’s<key>NSLocationAlwaysAndWhenInUseUsageDescription</key> <string>We need your location to track your journey even when the app is in the background.</string> <key>NSLocationWhenInUseUsageDescription</key> <string>We need your location to track your journey while you’re using the app.</string>Background Modes(target > Signing & Capabilities > Background Modes). And make sure you’re requesting theAlwayslocation permission from users—While Usingwon’t work for background tracking.
Your current config has stopOnTerminate: false (good!), but you’re missing a few critical flags:
const config: BackgroundGeolocationConfig = { desiredAccuracy: 10, stationaryRadius: 20, distanceFilter: 30, debug: true, stopOnTerminate: false, // Android-specific: Keep the plugin running as a foreground service startForeground: true, notificationTitle: "Tracking Your Location", notificationText: "Tap to open the app", // iOS-specific: Prevent system from pausing updates pausesLocationUpdatesAutomatically: false, stopOnStillActivity: false, // Don't stop tracking when device is stationary (adjust if needed) locationProvider: 2 // Use standard location provider for frequent updates (1 for significant changes) };
startForeground: trueis non-negotiable for Android—it prevents the system from killing the background service by showing a persistent notification.- For iOS,
locationProvider: 2gives you regular updates (use1only if you want low-power, infrequent significant location changes).
Most Android OEMs (Xiaomi, Huawei, OPPO) have aggressive battery savers that kill background apps. You’ll need to:
- Detect if your app is subject to battery optimization, then prompt users to add it to the whitelist or disable optimization for your app. You can use a plugin or native code to trigger the system settings screen for this.
- Avoid relying solely on the plugin’s default behavior—these OEMs often ignore standard foreground service rules unless the app is whitelisted.
Double-check that you’re starting the plugin correctly after the deviceready event (never start it before this!):
document.addEventListener("deviceready", () => { // Configure first BackgroundGeolocation.configure(config); // Listen for location updates BackgroundGeolocation.on("location", (location) => { console.log("Received background location:", location); // Send to your server here }); // Listen for errors to debug issues BackgroundGeolocation.on("error", (error) => { console.error("Background geolocation error:", error); }); // Start tracking—this is easy to forget! BackgroundGeolocation.start(); }, false);
Make sure BackgroundGeolocation.start() is always called, even if the app is launched in the background (some edge cases might skip this).
Simulators/emulators don’t replicate real background behavior reliably. iOS simulators, in particular, often fail to deliver background location updates. Always test on physical devices to get accurate results.
内容的提问来源于stack exchange,提问作者Arpit Goyal

