fix: battery optimization prompt — add missing manifest permission and fix fallback

REQUEST_IGNORE_BATTERY_OPTIMIZATIONS intent is silently dropped without
the matching manifest permission. Added it to app.json permissions.
Also replaced .catch() chain (which only triggers on thrown errors) with
try/catch blocks so the fallback to IGNORE_BATTERY_OPTIMIZATION_SETTINGS
actually fires. Added resetBatteryOptPrompt() helper to re-trigger the
prompt during testing.
This commit is contained in:
Davide Scaini
2026-06-03 09:18:03 +02:00
parent 765efe288e
commit 4e1c2ebef9
2 changed files with 22 additions and 10 deletions
+2 -1
View File
@@ -32,7 +32,8 @@
"BLUETOOTH", "BLUETOOTH",
"BLUETOOTH_ADMIN", "BLUETOOTH_ADMIN",
"BLUETOOTH_SCAN", "BLUETOOTH_SCAN",
"BLUETOOTH_CONNECT" "BLUETOOTH_CONNECT",
"REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"
] ]
}, },
"web": { "web": {
+20 -9
View File
@@ -4,6 +4,10 @@ import AsyncStorage from '@react-native-async-storage/async-storage';
const PROMPT_SHOWN_KEY = 'batteryOptPromptShown'; const PROMPT_SHOWN_KEY = 'batteryOptPromptShown';
export async function resetBatteryOptPrompt(): Promise<void> {
await AsyncStorage.removeItem(PROMPT_SHOWN_KEY);
}
export async function promptBatteryOptimizationIfNeeded(): Promise<void> { export async function promptBatteryOptimizationIfNeeded(): Promise<void> {
if (Platform.OS !== 'android') return; if (Platform.OS !== 'android') return;
@@ -18,16 +22,23 @@ export async function promptBatteryOptimizationIfNeeded(): Promise<void> {
[ [
{ {
text: 'Open settings', text: 'Open settings',
onPress: () => onPress: async () => {
IntentLauncher.startActivityAsync( try {
IntentLauncher.ActivityAction.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, await IntentLauncher.startActivityAsync(
{ data: 'package:com.bincio.rec' }, IntentLauncher.ActivityAction.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
).catch(() => { data: 'package:com.bincio.rec' },
);
} catch {
// Fallback: some OEMs don't support the direct intent — open the general page // Fallback: some OEMs don't support the direct intent — open the general page
IntentLauncher.startActivityAsync( try {
IntentLauncher.ActivityAction.IGNORE_BATTERY_OPTIMIZATION_SETTINGS, await IntentLauncher.startActivityAsync(
), IntentLauncher.ActivityAction.IGNORE_BATTERY_OPTIMIZATION_SETTINGS,
), );
} catch {
// Nothing more we can do
}
}
},
}, },
{ text: 'Later', style: 'cancel' }, { text: 'Later', style: 'cancel' },
], ],