From 4e1c2ebef9aa95985668c647039e137702e77087 Mon Sep 17 00:00:00 2001 From: Davide Scaini Date: Wed, 3 Jun 2026 09:18:03 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20battery=20optimization=20prompt=20?= =?UTF-8?q?=E2=80=94=20add=20missing=20manifest=20permission=20and=20fix?= =?UTF-8?q?=20fallback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- app.json | 3 ++- src/services/batteryOptimization.ts | 29 ++++++++++++++++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/app.json b/app.json index 521923b..5299a5b 100644 --- a/app.json +++ b/app.json @@ -32,7 +32,8 @@ "BLUETOOTH", "BLUETOOTH_ADMIN", "BLUETOOTH_SCAN", - "BLUETOOTH_CONNECT" + "BLUETOOTH_CONNECT", + "REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" ] }, "web": { diff --git a/src/services/batteryOptimization.ts b/src/services/batteryOptimization.ts index ad1dc9a..bf408b8 100644 --- a/src/services/batteryOptimization.ts +++ b/src/services/batteryOptimization.ts @@ -4,6 +4,10 @@ import AsyncStorage from '@react-native-async-storage/async-storage'; const PROMPT_SHOWN_KEY = 'batteryOptPromptShown'; +export async function resetBatteryOptPrompt(): Promise { + await AsyncStorage.removeItem(PROMPT_SHOWN_KEY); +} + export async function promptBatteryOptimizationIfNeeded(): Promise { if (Platform.OS !== 'android') return; @@ -18,16 +22,23 @@ export async function promptBatteryOptimizationIfNeeded(): Promise { [ { text: 'Open settings', - onPress: () => - IntentLauncher.startActivityAsync( - IntentLauncher.ActivityAction.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, - { data: 'package:com.bincio.rec' }, - ).catch(() => + onPress: async () => { + try { + await IntentLauncher.startActivityAsync( + IntentLauncher.ActivityAction.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, + { data: 'package:com.bincio.rec' }, + ); + } catch { // Fallback: some OEMs don't support the direct intent — open the general page - IntentLauncher.startActivityAsync( - IntentLauncher.ActivityAction.IGNORE_BATTERY_OPTIMIZATION_SETTINGS, - ), - ), + try { + await IntentLauncher.startActivityAsync( + IntentLauncher.ActivityAction.IGNORE_BATTERY_OPTIMIZATION_SETTINGS, + ); + } catch { + // Nothing more we can do + } + } + }, }, { text: 'Later', style: 'cancel' }, ],