Permission always returns never_ask_again - android

Steps to replicate:
react-native init project
<add code below to the project(App.js component)>
react-native run-android
I called the function in onPress of a text component.
The permission request always returns never_ask_again even for the fresh app run.
async requestPermissions() {
// android 6.0 +
if (Platform.OS === 'android' && Platform.Version >= 23) {
try {
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, {
'title': 'Let the access location',
'message': 'locate you.'
});
console.log('granted: ', granted);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
Alert.alert("Granted access", [{ text: "ok" }])
} else {
Alert.alert(
"can't access to GPS title",
"returned " + granted,
[
{ text: "ok" }
]
);
}
} catch (err) {
}
}
}
Even when the permissions is enabled from settings, still the never_ask_again is returned.
React Native Version: 0.55.2
React Version: 16.3.1
in app/build.gradle
targetSdkVersion 23
Thanks

One thing I missed was to add <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> in AppManifest.xml

Related

ReactNative and PermissionsAndroid 0.71 vs 0.70: POST_NOTIFICATION vs POST_NOTIFICATIONS and IllegalArgumentException

On https://reactnative.dev/docs/0.70/permissionsandroid it shows that this permission is available for verion 0.70:
POST_NOTIFICATION: 'android.permission.POST_NOTIFICATION'
On https://reactnative.dev/docs/permissionsandroid (version 0.71) it shows that the same persmission is then with an "S" at the end:
POST_NOTIFICATIONS: 'android.permission.POST_NOTIFICATIONS'
Is this a typo?
In either case, if I use any of the above, the app crashes with
IllegalArgumentException: permission is null
at android.app.ContextImpl.checkSelfPermission(ContextImpl.java:2222)
at android.content.ContextWrapper.checkSelfPermission(ContextWrapper.java:931)
at com.facebook.react.modules.permissions.PermissionsModule.requestPermission(PermissionsModule.java:105)
at java.lang.reflect.Method.invoke(Method.java)
when doing this:
async requestPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATION, // or POST_NOTIFICATIONS
{
'title': 'TEST',
'message': I18n.t('permissions.locationPermissionMessage')
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
} else {
}
} catch (err) {
}
}
The permission is in the AndroidManifest file, confirmed.
UPDATE
Set compileSdkVersion = 33 and targetSdkVersion = 33 in build.gradle:
code for requestPermission not looks like (I am using 0.70.5):
and the crash is noted like so:
Based on the source code, yes it's a typo that is fixed in 0.71.* and also see this pull request to know more details.
But about the crash, doesn't relate to typo in constants. First make sure to use compile sdk version 33. And get this permission for androids equal or greater than 13(API_LEVEL 33)
In order to check ANDROID_API_LEVEL in js side use DeviceInfo.
async requestPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATION, // or POST_NOTIFICATIONS
{
'title': 'TEST',
'message': I18n.t('permissions.locationPermissionMessage')
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
} else {
}
} catch (err) {
}
}
// where you need to get permission
if (Platform.OS == 'android' && DeviceInfo.getApiLevelSync() >= 33) {
await requestPermission();
}

React Native android permission is not showing

The react native android permission pop-up is not showing, I cannot download a file. It works on a simulator but not on a real phone.
Here is my permission function :
const requestAndroidStoragePermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
{
title: "File access",
message:
"The app needs files access to download your performance.",
}
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
this.setState({ storagePermission: true });
this.setState({ showExportModal: true });
} else {
console.log("Files permission denied");
}
} catch (err) {
console.warn(err);
}
};
I have the permission on my AndroidManifest.xml :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:targetSdkVersion="29" />
Someone have an idea ?

Dangerous Permissions is auto granted at app install in new project [React Native - Android]

I have installed awesome project from react native cli and run it in android build.
It works fine.
When I added, below dangerous permission
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
in my android manifest.xml and run the project it is auto granted on app install.
I check app info and it is granted by default.
Note:
Min Version : 23
Target and Compile Version : 29
React Native Version: 0.64.0
So when I check permission programmatically it is always granted. It should not.
from RN website :
(https://reactnative.dev/docs/permissionsandroid)
const requestPermissions = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.ACCESS_FINE_LOCATION,
{
title: "your title",
message:
"your message",
buttonNeutral: "Ask Me Later",
buttonNegative: "Cancel",
buttonPositive: "OK"
}
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("GPS permission ok");
} else {
console.log("GPS permission not ok");
}
} catch (err) {
console.warn(err);
}
};

rn-fetch-blob actionViewIntent() Does not work on Android 8.1 phones?

code
import RNFetchBlob from 'rn-fetch-blob';
RNFetchBlob.android
.actionViewIntent(
'/storage/emulated/0/Android/data/aaa.bbb.ccc/files/184.apk',
'application/vnd.android.package-archive',
)
.then(() => {
console.log('success');
})
.catch(err => {
console.log('error');
});
In the simulator, the above code works normally, and pops up the installation APK interface
On the phone (Android 8.1), the screen flashed white, then there was nothing, and the installation interface could not pop up, No error message
you should add permission in the AndroidMainFest.xml:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
but because it is dynamic permission, I suggest you every time before you download and install APK to check it if it has permission.
In this situation, you can write a native module, for dynamic check permission you can see this answer after it goes to the permission set page
you can also use the react-native-permission library.
requestInstallUpdate = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.REQUEST_INSTALL_PACKAGES,
{
'title': 'Test App',
'message': 'Test App needs to install updates.'
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("You can now install updates")
return true;
} else {
console.log("App install permission denied")
return false;
}
} catch (err) {
console.warn(err)
return false;
}
}
then in your download method
let canInstall = await requestInstallUpdate()
if(canInstall){
//download apk
}
Found out the reason for this, it's a know bug of the library and has a PR waiting to be merged (no timeframe from the repo owner). Here is the link to the PR: https://github.com/joltup/rn-fetch-blob/pull/317
So basically this needs to be added to line 122-123 of file android/src/main/java/com/RNFetchBlob/RNFetchBlob.java:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
If above is not working do to the below step: overwrite the 121 line in android/src/main/java/com/RNFetchBlob/RNFetchBlob.java:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 121 line
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // 122 line

How do I request permission for Android Device Location in React Native at run-time?

I have been trying to use React Native 's GeoLocalisation for an Android App. The poorly documentated module is found here https://facebook.github.io/react-native/docs/geolocation.html.
According to the documentation, you handle location permissions on Android using the following code in the AndroidManifest.xml file
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
However, my online research suggests that the above line of code is useless for versions of ANDROID >= 6.0
As my implementation of GeoLocation is not currently working, I have no other reason but to believe that location permissions are not correctly handled.
How do I successfully request location permission at run-time for React Native Android App?
I solved it by changing the targetSdkVersion ( same to compileSdkVersion, in my case 23) in android/app/build.gradle.
Edit AndroidManifest.xml located in android/src/main and add the
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
next :
import { PermissionsAndroid } from 'react-native';
and then add this method:
export async function requestLocationPermission()
{
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Example App',
'message': 'Example App access to your location '
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("You can use the location")
alert("You can use the location");
} else {
console.log("location permission denied")
alert("Location permission denied");
}
} catch (err) {
console.warn(err)
}
}
and access the method when you request the location at run-time
async componentWillMount() {
await requestLocationPermission()
}
This did not work for me
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
}
I referred to https://facebook.github.io/react-native/docs/permissionsandroid.html#request
and check() return a boolean
const granted = await PermissionsAndroid.check( PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION );
if (granted) {
console.log( "You can use the ACCESS_FINE_LOCATION" )
}
else {
console.log( "ACCESS_FINE_LOCATION permission denied" )
}
You could use the react native PermissionsAndroid to request the permission: https://facebook.github.io/react-native/docs/permissionsandroid.html#request
Or, an easier option will be using a library that does it for you, such as https://github.com/yonahforst/react-native-permissions
I've noticed two things:
You have to change compileSdkVersion 23 in build.gradle file
You have to add your View's click listener to display to Permission dialog.
Sample code:
import React, { Component } from 'react';
import { Text, PermissionsAndroid, Alert } from 'react-native';
export default class RuntimePermissionSample extends React.Component {
constructor(props) {
super(props);
}
async requestLocationPermission() {
const chckLocationPermission = PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);
if (chckLocationPermission === PermissionsAndroid.RESULTS.GRANTED) {
alert("You've access for the location");
} else {
try {
const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Cool Location App required Location permission',
'message': 'We required Location permission in order to get device location ' +
'Please grant us.'
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
alert("You've access for the location");
} else {
alert("You don't have access for the location");
}
} catch (err) {
alert(err)
}
}
};
render() {
return (
<Text
onPress={() => this.requestLocationPermission()}>
Request Location Permission
</Text>
)
}
}
Hope this would help you.
You can ask location permission using following code
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
alert("You can use the location")
} else {
alert("Location permission denied")
}
} catch (err) {
console.warn(err)
}
alert('hi');
in Manifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.CAMERA"/>
more details
import {PermissionsAndroid} from 'react-native';
async function requestCameraPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
{
title: 'Cool Photo App Camera Permission',
message:
'Cool Photo App needs access to your camera ' +
'so you can take awesome pictures.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('You can use the camera');
} else {
console.log('Camera permission denied');
}
} catch (err) {
console.warn(err);
}
}
export default class AlertDetails extends Component{
async componentDidMount() {
await request_storage_runtime_permission()
}
}

Categories

Resources