Open specific app's permission setting flutter? - android

I wonder how to open permission setting for specific app in Flutter? I search on internet but no article show me how to do that, just some solutions for android, like this. Open app permission settings

hi there try this code to ask for permission and open app settings like for location with the help of permission_handler(https://pub.dev/packages/permission_handler) package
Future<void> _request_permission(context,Function fn)async{
final Permission location_permission=Permission.location;
bool location_status=false;
bool ispermanetelydenied= await location_permission.isPermanentlyDenied;
if(ispermanetelydenied) {
print("denied");
await openAppSettings();
}else{
var location_statu = await location_permission.request();
location_status=location_statu.isGranted;
print(location_status);
}
}
Here this function allows you to open the app setting and manage permission in the case user permanently denied the permission
openAppSettings();
thanks

#azad-prajapat
And when I look at the package code I found this
/// Opens the app settings page.
///
/// Returns [true] if the app settings page could be opened, otherwise
/// [false].
Future<bool> openAppSettings() {
throw UnimplementedError('openAppSettings() has not been implemented.');
}

Related

Flutter permission_handler Doesnt ask for notification

I'm using firabase notifications. But before initialize it i want to ask user to get permission. But when i using permission_handler its not asking to user even i uninstall and reinstall app. How can i solve it ? its my code for ask it :
#override
void initState() {
getPermissions();
}
void getPermissions() async {
var requestResult = await Permission.notification.request();
var isPermissionGranted = await Permission.notification.isGranted;
var isPermissionPermamentlyDenied =
await Permission.notification.isPermanentlyDenied;
//Its giving logs immediatly. Doesn't ask for permission.
log("requestResult $requestResult");
log("isPermissionGranted $isPermissionGranted");
log("isPermissionPermamentlyDenied $isPermissionPermamentlyDenied");
The following permissions will show no dialog:
Notification
Bluetooth
The following permissions will show no dialog, but will open the corresponding setting intent for the user to change the permission status:
manageExternalStorage
systemAlertWindow
requestInstallPackages
accessNotificationPolicy
link: https://pub.dev/packages/permission_handler
Is your test device iOS or android?
If it's iOS, you have to configure in Xcode. Under Signing & Capabilities.
Add Push Notifications.

flutter location , Unhandled Exception: PlatformException(PERMISSION_DENIED_NEVER_ASK, Background location permission denied forever

I'm using Location flutter package to access the user's background location, first time any user opens the app it asks for permission, and when the user accepts it brings back this error in the console
Unhandled Exception: PlatformException(PERMISSION_DENIED_NEVER_ASK, Background location permission denied forever - please open app settings, null, null)
if the user closed the app and reopened it .. it works perfectly fine ( fetches location in both foreground and background) without even asking again for location permission.
Following the getting started guide in the package itself, here is how I added permission to my AndroidManifest.xml file :
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
I've been working on this issue and I discovered that the problem is when you use location.enableBackgroundMode() then choose always when you hit back first time it throws an exception and isBackgroundModeEnabled will be false too "despite the backgroundmode is enabled in system" and you have to restart the app to check if background mode is enabled correctly.
but, I've found that calling location.enableBackgroundMode() again is solving the issue too and it really doesn't ask for enabling background again but it somehow make isBackgroundModeEnabled return true value. Here's my fix code:
Future<bool> enableBackgroundMode() async {
bool _bgModeEnabled = await location.isBackgroundModeEnabled();
if (_bgModeEnabled) {
return true;
} else {
try {
await location.enableBackgroundMode();
} catch (e) {
debugPrint(e.toString());
}
try {
_bgModeEnabled = await location.enableBackgroundMode();
} catch (e) {
debugPrint(e.toString());
}
print(_bgModeEnabled); //True!
return _bgModeEnabled;
}
}

Permission request flutter app after denial

I'm developing a flutter application, I need to manage the permission request, but I don't know how to treat a particular occurrence:
If I deny two times the same permission through the popup it could be impossible approve it later 'cause the popup will not appair again.
Future<void> requestStoragePermission() async{
var status = await Permission.storage.status;
if(status.isPermanentlyDenied){
await AppSettings.openAppSettings();
} else {
await Permission.storage.request();
}
}
I don't understand how to distinguish when the permission has not yet been granted or when it has been refused several times because the function: Permission.storage.status always returns "denied".
****** EDIT ******
The problem arises when the user refuses the same permission several times (2 times) because the permissions request popup is no longer shown, in which case it is necessary to manually open the application settings and modify the permissions by hand. I have to make sure that: the first two times I request permissions with the popup then I should open the settings screen
I have always managed my permissions using two statuses granted and limited (used only for iOS14+). These two permissions are the only truethy statuses. All the others are falsey statuses.
the permission_handler package handles a lot of logic for you already. Before it makes the request, it will check the status to see if it is already defined. If it is, then it will return the status. If the permission has never been requested, then it will request the permission.
Personally, I set up a generic method for a permission request, to keep things DRY.
Future<bool> requestPermission(Permission setting) async {
// setting.request() will return the status ALWAYS
// if setting is already requested, it will return the status
final _result = await setting.request();
switch (_result) {
case PermissionStatus.granted:
case PermissionStatus.limited:
return true;
case PermissionStatus.denied:
case PermissionStatus.restricted:
case PermissionStatus.permanentlyDenied:
return false;
}
}
I then make a request like
final canUseStorage = await requestPermission(Permission.storage);
if (canUseStorage) {
// do something with storage
}
If you have UI that is dependent on a status from Permission, then you still call Permission.storage.status.
[EDIT]
At the moment, you can't track how many times the request pop-up has been shown via permission_handler. It only returns the status. You would need to take the user to the settings depending on the returned status value.
Side Note
Instead of taking the user directly to the settings. Maybe you show a pop up saying "Looks like we don't have permission...", with a button that the user can tap to go to the settings, provides the user with some context as to why they need to go to their settings. And it's also a better user experience!

PermissionHandler: Checking a permanently denied permission returns false unless requesting the permission is done first

This question is about permission handler package.
There are 2 methods in the library to check for a permission's status, for example in case of the microphone we can call:
PermissionStatus status1 = await Permission.microphone.status //call 1
PermissionStatus status2 = await Permission.microphone.request() //call 2
suppose a user presses the 'dont ask again' when he is prompted to grant the permission.
Now If:
we make call 1 to get the permission status, we should expect to get PermissionStatus.permanentlyDenied but the result is PermissionStatus.denied
we make call 2 to get the permission status, we expect to get PermissionStatus.permanentlyDenied and without the dialouge appearing again to the user, and this is exactly what happens
So call 2 works as intended but call 1 works as if there are only 2 states for a permission that can be returned from this call: granted or denied. But then how can we get the real status of the permission (for example it may be deniedPermanently) without calling request() in this case? Is this a bug or is this the intended behaviour? Sometimes I need to just check the status of the permission but I don't want to open a permission dialouge to the user, how to do that?
I will explain my use-case breifly:
I call the Permission.microphone.request() first time and then update my widget's state, then if the app (in android: activity) is paused, in the didChangeAppLifecycleState method which comes from the WidgetsBindingObserver mixin, I do check if the permission was revoked or permanently denied using Permission.microphone.status, but the problem is that this method is returning a denied status even when the permission is permanentlyDenied, and the correct return value which is permanentlyDenied is only returned if I call Permission.microphone.request()
code demo:
PermissionStatus result = await Permission.microphone.request();//this returns permanentlyDenied without opening the dialouge => ok
PermissionStatus result = await Permission.microphone.status;//this returns denied even if the status was permanently denied without opening the dialouge => NOT OK

Progress Web App Prompt choise problem in not chrome browsers

I try to publish a web page as as PWA app with SAMSUNG internet.
I would like to use a custom "Add to home screen" pop up. For this i have to use the following event and i like to listen to the user choice.
In Chrome the user choice does fire after the user choice...
but in SAMSUNG internet it doesn't react to this...
... can somebody helps me with this?
Thanks
window.addEventListener('beforeinstallprompt', function (e) {
deferredPrompt = e;
deferredPrompt.preventDefault();
deferredPrompt.then((choice) => {
if (choice.outcome === 'accepted') {
console.log('User accepted the A2HS prompt');
} else {
console.log('User dismissed the A2HS prompt');
}
// Clear the saved prompt since it can't be used again
installPromptEvent = null;
});
})
From caniuse Samsung internet does not support the beforeinstallprompt and appinstalled events.
Ref: https://caniuse.com/#feat=web-app-manifest
So you won't be able to use it this way at all.
caniuse screenshot as of 2020-04-13

Categories

Resources