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:
@@ -32,7 +32,8 @@
|
||||
"BLUETOOTH",
|
||||
"BLUETOOTH_ADMIN",
|
||||
"BLUETOOTH_SCAN",
|
||||
"BLUETOOTH_CONNECT"
|
||||
"BLUETOOTH_CONNECT",
|
||||
"REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"
|
||||
]
|
||||
},
|
||||
"web": {
|
||||
|
||||
@@ -4,6 +4,10 @@ import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
|
||||
const PROMPT_SHOWN_KEY = 'batteryOptPromptShown';
|
||||
|
||||
export async function resetBatteryOptPrompt(): Promise<void> {
|
||||
await AsyncStorage.removeItem(PROMPT_SHOWN_KEY);
|
||||
}
|
||||
|
||||
export async function promptBatteryOptimizationIfNeeded(): Promise<void> {
|
||||
if (Platform.OS !== 'android') return;
|
||||
|
||||
@@ -18,16 +22,23 @@ export async function promptBatteryOptimizationIfNeeded(): Promise<void> {
|
||||
[
|
||||
{
|
||||
text: 'Open settings',
|
||||
onPress: () =>
|
||||
IntentLauncher.startActivityAsync(
|
||||
onPress: async () => {
|
||||
try {
|
||||
await IntentLauncher.startActivityAsync(
|
||||
IntentLauncher.ActivityAction.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
|
||||
{ data: 'package:com.bincio.rec' },
|
||||
).catch(() =>
|
||||
);
|
||||
} catch {
|
||||
// Fallback: some OEMs don't support the direct intent — open the general page
|
||||
IntentLauncher.startActivityAsync(
|
||||
try {
|
||||
await IntentLauncher.startActivityAsync(
|
||||
IntentLauncher.ActivityAction.IGNORE_BATTERY_OPTIMIZATION_SETTINGS,
|
||||
),
|
||||
),
|
||||
);
|
||||
} catch {
|
||||
// Nothing more we can do
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
{ text: 'Later', style: 'cancel' },
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user