react-native: permission is null in react native app
Description
When I requested MANAGE_EXTERNAL_STORAGE getting a null permission error. in react native app. when I went throw the document (https://reactnative.dev/docs/permissionsandroid) there is no permission like this, but actually, for accessing hidden files we need this permission. If I gave the same permission in the androidmanifest.xml file not even asking the user.
enter the image description here
packages used: “react”: “^17.0.2”, “react-native”: “^0.68.2”,
Version
0.68.2
Output of npx react-native info
npm WARN config global --global
, --local
are deprecated. Use --location=global
instead.
info Fetching system and libraries information…
System:
OS: Windows 10 10.0.19044
CPU: (4) x64 Intel® Core™ i3-2328M CPU @ 2.20GHz
Memory: 887.69 MB / 5.90 GB
Binaries:
Node: 16.16.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.4 - ~\AppData\Roaming\npm\yarn.CMD
npm: 6.14.6 - C:\Program Files\nodejs\npm.CMD
Watchman: Not Found
SDKs:
Android SDK: Not Found
Windows SDK: Not Found
IDEs:
Android Studio: Version 4.0.0.0 AI-193.6911.18.40.6514223
Visual Studio: Not Found
Languages:
Java: 14.0.1 - C:\Program Files\Java\jdk-14.0.1\bin\javac.EXE
npmPackages:
@react-native-community/cli: Not Found
react: ^17.0.2 => 17.0.2
react-native: ^0.68.2 => 0.68.2
react-native-windows: Not Found
npmGlobalPackages:
react-native: Not Found
Steps to reproduce
please use the same packages and request MANAGE_EXTERNAL_STORAGE permission like the below code.
const pm1 = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE); // const pm2 = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE); const pm3 = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.MANAGE_EXTERNAL_STORAGE); // ACCESS_MEDIA_LOCATION
if (pm1 && pm2) return true
const userResponse = await PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE.at,
PermissionsAndroid.PERMISSIONS.MANAGE_EXTERNAL_STORAGE
]);
Snack, code example, screenshot, or link to a repository
manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
code :
const pm1 = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE); //
const pm2 = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE);
const pm3 = await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.MANAGE_EXTERNAL_STORAGE); // ACCESS_MEDIA_LOCATION
if (pm1 && pm2) return true
const userResponse = await PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE.at,
PermissionsAndroid.PERMISSIONS.MANAGE_EXTERNAL_STORAGE
]);
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 9
- Comments: 20
If you’re working on one of the latest version of React Native it seems the problem got fixed on 0.70.7. For the rest of the unlucky mortals there is a workaround fortunately…
In order to fix this I had to create a patch using patch-package along with postinstall and update my current PermissionAndroid files to support in my case
POST_NOTIFICATIONS
given I’m using react-native 0.64.4. So I went ahead and updated these 2 files as detailed below:After this change the PermissionsAndroid module from react-native started to work again as expected without getting any
permission is null
error.BTW also don’t forget to include the respective permission on the
AndroidManifest
, in my case was:<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
Finally be sure to set the
compileSdkVersion
and thetargetSdkVersion
to33
for the permission prompt to show up…My case found this error because using react-native 0.70.4 It’s map key like below at node_modules/react-native/Libraries/PermissionsAndroid/PermissionsAndroid.js
POST_NOTIFICATION: 'android.permission.POST_NOTIFICATIONS',
Then exactly It’s should be PermissionsAndroid.PERMISSIONS.POST_NOTIFICATION (Not POST_NOTIFICATIONS)
For android 33, I did this
PermissionsAndroid.request('android.permission.POST_NOTIFICATIONS');
I get a red line saying
But it still works so I just ignore this error. To fix this, I would have to upgrade my react native to a version that expects this POST_NOTIFICATION value, but I am not ready to do that upgrade quite yet.
from FCM documentation For API level 33+ you will need to request the permission manually using either the built-in react-native PermissionsAndroid APIs or a related module such as react-native-permissions
I used react-native-permissions instead of patching,
add this permission in android manifest file <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
You have to target at least SDK 33 to perform request on Android 13+. The permission is always granted for prior versions.
buildscript { ext { buildToolsVersion = “33.0.0” // <- set at least 33.x minSdkVersion = 21 compileSdkVersion = 33 // <- set at least 33 targetSdkVersion = 33 // <- set at least 33
import {Platform} from ‘react-native’; const deviceAPiLevel = Platform.Version; if (Platform.OS === ‘android’ && deviceAPiLevel >= 33) { await request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS); }
I was on an older version of react-native 0.66.5
I realized that PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS is not exported in this react-native version so I added this manually like this.
@jaaywags Thank a Lot Bro… Your Rocking…
MY worked versions :
buildToolsVersion = "30.0.3" minSdkVersion = 21 compileSdkVersion = 33 targetSdkVersion = 33
I am using this const status = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.POST_NOTIFICATION, ); But it works fine on my emulator but it never asks for permission on a real device and the by default status consoles out as never_ask_again without even asking for permission. Maybe there is a conflict for the Android Version? If yes please guide me on how can I tackle it.
But there is another thing if I handleListeners for push notifications with permission status as never_ask_again and I send a notification from testfcm.com when the app is not active it works but is not in the active state for the first time.