When should I use battery optimization exemption vs foreground service?
When should I use battery optimization exemption vs foreground service?
Battery optimization exemption allows your app to:
- Use the network during Doze mode
- Hold partial wake locks even when the device is idle
- Receive alarms and network callbacks during maintenance windows
- Apps that need occasional background network access
- Location tracking with intermittent updates
- Background tasks that run periodically
- Your app needs continuous background execution
- You’re running long-duration tasks (music playback, navigation, fitness tracking)
- You want to prevent the app from entering App Standby
- You need a persistent notification to indicate active background work
react-native-background-guardian handles wake locks and battery settings, but you’ll need a library like react-native-background-actions or native code to implement foreground services.What's the difference between Power Save mode and battery optimization?
What's the difference between Power Save mode and battery optimization?
These are two separate system features:Battery optimization (Doze mode):
- Per-app setting
- Can be disabled for individual apps
- Check with
isIgnoringBatteryOptimizations() - Request exemption with
requestBatteryOptimizationExemption() - When disabled, your app can hold wake locks and access network during Doze
- System-wide setting
- Affects ALL apps regardless of their battery optimization status
- Check with
isPowerSaveMode() - Cannot be programmatically disabled (user must manually turn it off)
- When active, the system throttles network, location, and background jobs
isIgnoringBatteryOptimizations() = true) but still be affected by Power Save mode restrictions. Always check both settings for critical background tasks.How does this library work on iOS?
How does this library work on iOS?
All methods are safe no-ops on iOS that return appropriate default values:
acquireWakeLock()→ returnstrue(no-op)releaseWakeLock()→ returnstrue(no-op)isWakeLockHeld()→ returnsfalseenableScreenWakeLock()→ disables idle timer (screen stays on)disableScreenWakeLock()→ re-enables idle timerisIgnoringBatteryOptimizations()→ returnstruegetDeviceManufacturer()→ returns"Apple"- All other methods → return
falseortrueas appropriate
What are Google Play policies around REQUEST_IGNORE_BATTERY_OPTIMIZATIONS?
What are Google Play policies around REQUEST_IGNORE_BATTERY_OPTIMIZATIONS?
Google Play restricts the
If your app doesn’t fit these categories, use vs.
REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission to specific use cases. Your app must fall into one of these categories:| App type | Description |
|---|---|
| Chat / Voice / Video | Apps needing real-time messaging where FCM High Priority is insufficient |
| Task automation | Apps that schedule automated actions (macros) |
| Health / Fitness | Tracking workouts (often combined with foreground service) |
| Device connection | Companion apps for smartwatches, IoT devices, etc. |
| Safety | Apps for personal safety (SOS) |
| VPN / Proxy | Network tools |
openBatteryOptimizationSettings() instead of requestBatteryOptimizationExemption(). This opens the settings list where users can manually toggle battery optimization without requiring the restricted permission.Safer alternative:How do I test Doze mode during development?
How do I test Doze mode during development?
You can force your device into Doze mode using ADB:Testing Power Save mode:
- Connect your device via USB and enable USB debugging
- Unplug the charger (Doze only activates on battery power)
-
Force Doze mode:
-
Check if Doze is active in your app:
-
Exit Doze mode when done testing:
- Enable Battery Saver in device settings
- Use
isPowerSaveMode()to verify detection - Test your app’s behavior when both Doze and Power Save are active
How do I test on different OEM devices?
How do I test on different OEM devices?
Testing on actual devices is critical because OEM battery optimization behavior varies significantly:Recommended test devices:
- Xiaomi (MIUI) - Most aggressive
- Samsung (OneUI) - Moderately aggressive
- Google Pixel (Stock Android) - Standard Android behavior
- Huawei/Honor (if targeting those markets)
- One device from Oppo/Vivo/Realme/OnePlus family
-
Check manufacturer detection:
-
Test OEM settings:
-
Test background survival:
- Grant all battery permissions
- Acquire wake lock
- Lock screen and wait 5-10 minutes
- Verify app still running
-
Test without permissions:
- Revoke battery exemptions
- Test background behavior
- Verify graceful degradation
Why is my wake lock not working?
Why is my wake lock not working?
Common issues and solutions:1. Testing on emulator
- Emulators may not accurately simulate wake lock behavior
- Always test on real devices
- The library automatically adds
WAKE_LOCKpermission, but verify it’s in your merged manifest:
- Even with wake locks, aggressive Doze mode can limit effectiveness
- Check status:
isIgnoringBatteryOptimizations() - Request exemption if needed
- Wake locks have reduced effectiveness when Battery Saver is on
- Check with
isPowerSaveMode() - Guide users to disable it for critical tasks
- Xiaomi, Huawei, and other manufacturers have additional restrictions
- Use
openOEMSettings()to guide users to OEM-specific settings - Enable “Autostart” and disable manufacturer battery optimization
- Default timeout is 24 hours
- Increase timeout for longer tasks:
acquireWakeLock("tag", 48 * 60 * 60 * 1000) - Re-acquire wake lock periodically for indefinite tasks
- Always call
releaseWakeLock()when done - Use try/finally blocks to ensure cleanup
Should I use requestBatteryOptimizationExemption() or openBatteryOptimizationSettings()?
Should I use requestBatteryOptimizationExemption() or openBatteryOptimizationSettings()?
Choose based on your app’s use case and Google Play policy requirements:Use Use Safe approach for all apps:This approach works for any app type and avoids Google Play policy issues.
requestBatteryOptimizationExemption() when:- Your app falls into an acceptable use case (messaging, health, device management, etc.)
- You want a streamlined user experience with a direct “Allow” dialog
- You’ve verified Google Play policy compliance
- You’re willing to add the
REQUEST_IGNORE_BATTERY_OPTIMIZATIONSpermission
openBatteryOptimizationSettings() when:- Your app might not meet Google Play’s acceptable use cases
- You want to avoid policy scrutiny
- You don’t need the restricted permission
- You’re okay with a slightly more manual user flow
Does this library work with the New Architecture?
Does this library work with the New Architecture?
Yes! React Native Background Guardian is built as a Turbo Module and fully supports the New Architecture.The library works with:
- Old Architecture (Bridge)
- New Architecture (Turbo Modules)
- Both Android and iOS
- React Native 0.68+
How do I refresh battery optimization status after the user returns from settings?
How do I refresh battery optimization status after the user returns from settings?
When you open system settings, your app goes to the background. Use This ensures your UI always reflects the current battery optimization status.
AppState to detect when the user returns and refresh the status:
