expo-ignore-battery-optimizations
v0.2.0
Published
android ignore battery optimizations
Maintainers
Readme
Expo Ignore Battery Optimizations
Check and request the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission in Android.
Table of Contents
Installation
npm install expo-ignore-battery-optimizations
pnpm install expo-ignore-battery-optimizations
yarn add expo-ignore-battery-optimizationsConfigure for Android
plugin
// app.json
{
"expo": {
"plugins": ["expo-ignore-battery-optimizations"]
}
}manual permissions
// app.json
{
"expo": {
"android": {
"permissions": ["REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"]
},
}
}Direct API
import {
isIgnoringBatteryOptimizations,
requestIgnoreBatteryOptimizations,
} from 'expo-ignore-battery-optimizations';
const ignored = isIgnoringBatteryOptimizations();
if (!ignored) {
await requestIgnoreBatteryOptimizations();
}isIgnoringBatteryOptimizations() returns:
truewhen the app is already exempt from battery optimizationsfalseon Android when the exemption is not activefalseon non-Android platforms
requestIgnoreBatteryOptimizations():
- opens the system settings flow on Android
- is a no-op on non-Android platforms
Usage
import { useEffect } from 'react';
import { View, Alert } from 'react-native';
import { useIgnoreBatteryOptimizationPermission } from 'expo-ignore-battery-optimizations';
export default function App() {
const { hasPermission, requestPermission } = useIgnoreBatteryOptimizationPermission();
useEffect(() => {
if (!hasPermission) {
Alert.alert(
'Battery Optimization',
'To ensure the app works properly, please allow it to ignore battery optimizations.',
[
{
text: 'Cancel',
style: 'cancel',
},
{
text: 'Allow',
onPress: requestPermission,
},
],
);
}
}, [hasPermission, requestPermission]);
return <View />;
}useIgnoreBatteryOptimizationPermission() returns:
status:'ignored' | 'not-ignored'hasPermission:truewhen battery optimizations are already ignoredcanRequestPermission:truewhen the permission can still be requested on AndroidrequestPermission(): opens the system settings flow and resolves after the app returns, after the hook re-checks the current permissionstatus/hasPermission: hook-managed state that updates when the app becomes active again
On non-Android platforms:
statusstaysnot-ignoredcanRequestPermissionisfalserequestPermission()resolves tofalsewithout opening settings
Why Use This?
Some Android device manufacturers aggressively limit background activity to save battery. To improve reliability of background services (e.g., location tracking, push messaging, etc.), your app may request the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission.
⚠️ Note: Requesting this permission does not guarantee users will approve it, and it requires clear justification to avoid Play Store policy violations.
This intent is available only on Android 6.0 (API level 23) and above.
Contributing
Pull requests are welcome! If you’d like to fix a bug or propose a feature, feel free to open an issue or PR.
