Search code examples
javascriptandroidreact-nativeandroid-permissions

How to get grant staus from battery optimization permission popup


I am using https://www.npmjs.com/package/react-native-disable-battery-optimizations-android package for my React Native Android app. This package provides OpenBatteryModal() method to raise popup to get ignore battery optimization permission. But it does not provide how to get permission granted status. So, my question is "Is there any way to get to know what option is selected in permission popup or is there any other workaround in this package to get the grant status? I am struck on this for days. There is not much help also apart from using this package.


Solution

  • It looks like this library (RNDisableBatteryOptimizationsAndroid) has a method to check the status of battery optimization, it's even mentioned on their README under usage. I'm not sure what you've tried, but this is how you can check if the permission was granted using their isBatteryOptimizationEnabled API:

    import RNDisableBatteryOptimizationsAndroid from 'react-native-disable-battery-optimizations-android';
    
    RNDisableBatteryOptimizationsAndroid.isBatteryOptimizationEnabled().then((isEnabled)=>{
        if(isEnabled){
            RNDisableBatteryOptimizationsAndroid.openBatteryModal();
        }
    });
    

    You can't get the user selected option directly, so in case the above example is not enough - you can write some logic to get the status when your app becomes active (using react-native AppState) after the modal is closed; since the modal is a new activity you should be notified when your activity becomes active again. Optimize it so you only check the status if openBatteryModal was requested.