react-native icon indicating copy to clipboard operation
react-native copied to clipboard

permission is null in react native app

Open nivas412 opened this issue 2 years ago • 22 comments

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 image

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(R) Core(TM) 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
    ]);

nivas412 avatar Aug 14 '22 05:08 nivas412

That permission doesn't seem to exist in the codebase. Related: How do I ask the user to enable the MANAGE_EXTERNAL_STORAGE permission in React Native

JosuGZ avatar Aug 26 '22 20:08 JosuGZ

As of my knowledge there are only two permissions for Android storage , READ AND WRITE

suyashvash avatar Aug 28 '22 13:08 suyashvash

As of my knowledge there are only two permissions for Android storage , READ AND WRITE

If you check my link you will see that what you say is not true anymore.

JosuGZ avatar Aug 28 '22 17:08 JosuGZ

Any news or workaround on this one?

sideshowbob avatar Feb 24 '23 14:02 sideshowbob

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:

  • /node_modules/react-native/Libraries/PermissionsAndroid/NativePermissionsAndroid.js
...
| 'android.permission.RECEIVE_WAP_PUSH'
| 'android.permission.RECEIVE_MMS'
| 'android.permission.WRITE_EXTERNAL_STORAGE'; <- removed this and
| 'android.permission.WRITE_EXTERNAL_STORAGE' <- added these 2
| 'android.permission.POST_NOTIFICATIONS';
...
  • /node_modules/react-native/Libraries/PermissionsAndroid/PermissionsAndroid.js
...
RECEIVE_MMS: 'android.permission.RECEIVE_MMS',
READ_EXTERNAL_STORAGE: 'android.permission.READ_EXTERNAL_STORAGE',
WRITE_EXTERNAL_STORAGE: 'android.permission.WRITE_EXTERNAL_STORAGE',
POST_NOTIFICATIONS: 'android.permission.POST_NOTIFICATIONS', <- added this
...
WRITE_CALL_LOG: string,
WRITE_CONTACTS: string,
WRITE_EXTERNAL_STORAGE: string,
POST_NOTIFICATIONS: string, <- added this
....

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 the targetSdkVersion to 33 for the permission prompt to show up...

federicomiralles avatar Feb 28 '23 18:02 federicomiralles

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)

import {PermissionsAndroid} from 'react-native';
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATION);

jedsada-jed avatar Apr 16 '23 05:04 jedsada-jed

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:

  • /node_modules/react-native/Libraries/PermissionsAndroid/NativePermissionsAndroid.js
...
| 'android.permission.RECEIVE_WAP_PUSH'
| 'android.permission.RECEIVE_MMS'
| 'android.permission.WRITE_EXTERNAL_STORAGE'; <- removed this and
| 'android.permission.WRITE_EXTERNAL_STORAGE' <- added these 2
| 'android.permission.POST_NOTIFICATIONS';
...
  • /node_modules/react-native/Libraries/PermissionsAndroid/PermissionsAndroid.js
...
RECEIVE_MMS: 'android.permission.RECEIVE_MMS',
READ_EXTERNAL_STORAGE: 'android.permission.READ_EXTERNAL_STORAGE',
WRITE_EXTERNAL_STORAGE: 'android.permission.WRITE_EXTERNAL_STORAGE',
POST_NOTIFICATIONS: 'android.permission.POST_NOTIFICATIONS', <- added this
...
WRITE_CALL_LOG: string,
WRITE_CONTACTS: string,
WRITE_EXTERNAL_STORAGE: string,
POST_NOTIFICATIONS: string, <- added this
....

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 the targetSdkVersion to 33 for the permission prompt to show up...

Thanks sooooooo much !!!!!!!!!

Gautham495 avatar Jun 02 '23 19:06 Gautham495

I tried all above solutions but still the error persists...

Actually, For me 'PermissionStatus' and 'PermissionType' is already commented as below in 'NativePermissionAndroid.js' file.

import type {TurboModule} from '../TurboModule/RCTExport';
import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry';

// TODO: Use proper enum types.
export type PermissionStatus = string;
export type PermissionType = string;
/*
export type PermissionStatus = 'granted' | 'denied' | 'never_ask_again';
export type PermissionType =
  | 'android.permission.READ_CALENDAR'
  | 'android.permission.PROCESS_OUTGOING_CALLS'
  | 'android.permission.BODY_SENSORS'
  | 'android.permission.SEND_SMS'
  | 'android.permission.RECEIVE_SMS'
  | 'android.permission.READ_SMS'
  | 'android.permission.RECEIVE_WAP_PUSH'
  | 'android.permission.RECEIVE_MMS'
  | 'android.permission.READ_EXTERNAL_STORAGE'
  | 'android.permission.WRITE_EXTERNAL_STORAGE';
*/
 

Any Suggestions / help would be highly appreciable...

amitasort123 avatar Jun 28 '23 05:06 amitasort123

react-native must be 0.70+

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:

  • /node_modules/react-native/Libraries/PermissionsAndroid/NativePermissionsAndroid.js
...
| 'android.permission.RECEIVE_WAP_PUSH'
| 'android.permission.RECEIVE_MMS'
| 'android.permission.WRITE_EXTERNAL_STORAGE'; <- removed this and
| 'android.permission.WRITE_EXTERNAL_STORAGE' <- added these 2
| 'android.permission.POST_NOTIFICATIONS';
...
  • /node_modules/react-native/Libraries/PermissionsAndroid/PermissionsAndroid.js
...
RECEIVE_MMS: 'android.permission.RECEIVE_MMS',
READ_EXTERNAL_STORAGE: 'android.permission.READ_EXTERNAL_STORAGE',
WRITE_EXTERNAL_STORAGE: 'android.permission.WRITE_EXTERNAL_STORAGE',
POST_NOTIFICATIONS: 'android.permission.POST_NOTIFICATIONS', <- added this
...
WRITE_CALL_LOG: string,
WRITE_CONTACTS: string,
WRITE_EXTERNAL_STORAGE: string,
POST_NOTIFICATIONS: string, <- added this
....

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 the targetSdkVersion to 33 for the permission prompt to show up...

worked for me, using react native 0.70.6 thanks

Prashant-Verma-5c avatar Jul 15 '23 14:07 Prashant-Verma-5c

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.

hamza4213 avatar Aug 15 '23 11:08 hamza4213

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

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 {PERMISSIONS, request} from 'react-native-permissions';

import {Platform} from 'react-native'; const deviceAPiLevel = Platform.Version; if (Platform.OS === 'android' && deviceAPiLevel >= 33) { await request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS); }

umairm1alik avatar Aug 17 '23 06:08 umairm1alik

For android 33, I did this PermissionsAndroid.request('android.permission.POST_NOTIFICATIONS');

I get a red line saying

Argument of type '"android.permission.POST_NOTIFICATIONS"' is not assignable to parameter of type 'Permission'

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.

jaaywags avatar Aug 22 '23 17:08 jaaywags

Thank bro. It 's worked

HoanNguyen135 avatar Aug 28 '23 08:08 HoanNguyen135

@jaaywags Thank a Lot Bro.... Your Rocking...

MY worked versions :

buildToolsVersion = "30.0.3" minSdkVersion = 21 compileSdkVersion = 33 targetSdkVersion = 33

    And Permission requesting Type : 
    
    
    ` PermissionsAndroid.request("android.permission.POST_NOTIFICATIONS")
  .then((res) => {
    LOG("permission for notification enabled");
  })
  .catch((err) => {
    LOG("permission for notification canceled :", err);
  });`

Vigneshwaran-crypto avatar Nov 01 '23 04:11 Vigneshwaran-crypto

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.

PermissionsAndroid.check('android.permission.POST_NOTIFICATIONS').then(
  async response => {

    console.log('Notification: ', response, PermissionsAndroid.PERMISSIONS);

    if(!response){
      await PermissionsAndroid.request('android.permission.POST_NOTIFICATIONS');
    }
  }
)

mawais78 avatar Dec 12 '23 10:12 mawais78

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:

  • /node_modules/react-native/Libraries/PermissionsAndroid/NativePermissionsAndroid.js
...
| 'android.permission.RECEIVE_WAP_PUSH'
| 'android.permission.RECEIVE_MMS'
| 'android.permission.WRITE_EXTERNAL_STORAGE'; <- removed this and
| 'android.permission.WRITE_EXTERNAL_STORAGE' <- added these 2
| 'android.permission.POST_NOTIFICATIONS';
...
  • /node_modules/react-native/Libraries/PermissionsAndroid/PermissionsAndroid.js
...
RECEIVE_MMS: 'android.permission.RECEIVE_MMS',
READ_EXTERNAL_STORAGE: 'android.permission.READ_EXTERNAL_STORAGE',
WRITE_EXTERNAL_STORAGE: 'android.permission.WRITE_EXTERNAL_STORAGE',
POST_NOTIFICATIONS: 'android.permission.POST_NOTIFICATIONS', <- added this
...
WRITE_CALL_LOG: string,
WRITE_CONTACTS: string,
WRITE_EXTERNAL_STORAGE: string,
POST_NOTIFICATIONS: string, <- added this
....

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 the targetSdkVersion to 33 for the permission prompt to show up...

Thank you, it's working for me. Could you also provide the patch files? I'm not familiar with creating patch files.

I created the below patch file named POST_NOTIFICATIONS.patch but it says Unrecognized patch file in patches directory POST_NOTIFICATIONS.patch

diff --git a/node_modules/react-native/Libraries/PermissionsAndroid/NativePermissionsAndroid.js b/node_modules/react-native/Libraries/PermissionsAndroid/NativePermissionsAndroid.js
index 358882c..01915b6 100644
--- a/node_modules/react-native/Libraries/PermissionsAndroid/NativePermissionsAndroid.js
+++ b/node_modules/react-native/Libraries/PermissionsAndroid/NativePermissionsAndroid.js
@@ -42,6 +42,7 @@ export type PermissionType =
   | 'android.permission.RECEIVE_MMS'
   | 'android.permission.READ_EXTERNAL_STORAGE'
   | 'android.permission.WRITE_EXTERNAL_STORAGE'
+  | 'android.permission.POST_NOTIFICATIONS'
   | 'android.permission.BLUETOOTH_CONNECT'
   | 'android.permission.BLUETOOTH_SCAN'
   | 'android.permission.BLUETOOTH_ADVERTISE';
diff --git a/node_modules/react-native/Libraries/PermissionsAndroid/PermissionsAndroid.js b/node_modules/react-native/Libraries/PermissionsAndroid/PermissionsAndroid.js
index 1006cd3..12dd9f8 100644
--- a/node_modules/react-native/Libraries/PermissionsAndroid/PermissionsAndroid.js
+++ b/node_modules/react-native/Libraries/PermissionsAndroid/PermissionsAndroid.js
@@ -59,6 +59,7 @@ const PERMISSIONS = Object.freeze({
   RECEIVE_MMS: 'android.permission.RECEIVE_MMS',
   READ_EXTERNAL_STORAGE: 'android.permission.READ_EXTERNAL_STORAGE',
   WRITE_EXTERNAL_STORAGE: 'android.permission.WRITE_EXTERNAL_STORAGE',
+  POST_NOTIFICATIONS: 'android.permission.POST_NOTIFICATIONS',
   BLUETOOTH_CONNECT: 'android.permission.BLUETOOTH_CONNECT',
   BLUETOOTH_SCAN: 'android.permission.BLUETOOTH_SCAN',
   BLUETOOTH_ADVERTISE: 'android.permission.BLUETOOTH_ADVERTISE',
@@ -100,6 +101,7 @@ class PermissionsAndroid {
     WRITE_CALL_LOG: string,
     WRITE_CONTACTS: string,
     WRITE_EXTERNAL_STORAGE: string,
+    POST_NOTIFICATIONS: string,
   |} = PERMISSIONS;
   RESULTS: {|
     DENIED: $TEMPORARY$string<'denied'>,

Robiullah2244 avatar Dec 20 '23 20:12 Robiullah2244

I have this problem with notification permission when i upgrade to react native 0.73.4. I have this:

if (Platform.OS === "android" && deviceAPiLevel >= 33) { try { await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATION); } catch (error) { console.log("error permission android", error); } }

manuelabarca avatar Feb 08 '24 08:02 manuelabarca

For me problem was that app was freshly installed and app had not permissions. When to: 'App info' give permissions app requests and it will start working.

Aleksa-startify avatar Feb 22 '24 16:02 Aleksa-startify

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:

  • /node_modules/react-native/Libraries/PermissionsAndroid/NativePermissionsAndroid.js
...
| 'android.permission.RECEIVE_WAP_PUSH'
| 'android.permission.RECEIVE_MMS'
| 'android.permission.WRITE_EXTERNAL_STORAGE'; <- removed this and
| 'android.permission.WRITE_EXTERNAL_STORAGE' <- added these 2
| 'android.permission.POST_NOTIFICATIONS';
...
  • /node_modules/react-native/Libraries/PermissionsAndroid/PermissionsAndroid.js
...
RECEIVE_MMS: 'android.permission.RECEIVE_MMS',
READ_EXTERNAL_STORAGE: 'android.permission.READ_EXTERNAL_STORAGE',
WRITE_EXTERNAL_STORAGE: 'android.permission.WRITE_EXTERNAL_STORAGE',
POST_NOTIFICATIONS: 'android.permission.POST_NOTIFICATIONS', <- added this
...
WRITE_CALL_LOG: string,
WRITE_CONTACTS: string,
WRITE_EXTERNAL_STORAGE: string,
POST_NOTIFICATIONS: string, <- added this
....

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 the targetSdkVersion to 33 for the permission prompt to show up...

This works on my local, but should work also after I made the release?

brunoosella avatar Mar 22 '24 17:03 brunoosella

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:

  • /node_modules/react-native/Libraries/PermissionsAndroid/NativePermissionsAndroid.js
...
| 'android.permission.RECEIVE_WAP_PUSH'
| 'android.permission.RECEIVE_MMS'
| 'android.permission.WRITE_EXTERNAL_STORAGE'; <- removed this and
| 'android.permission.WRITE_EXTERNAL_STORAGE' <- added these 2
| 'android.permission.POST_NOTIFICATIONS';
...
  • /node_modules/react-native/Libraries/PermissionsAndroid/PermissionsAndroid.js
...
RECEIVE_MMS: 'android.permission.RECEIVE_MMS',
READ_EXTERNAL_STORAGE: 'android.permission.READ_EXTERNAL_STORAGE',
WRITE_EXTERNAL_STORAGE: 'android.permission.WRITE_EXTERNAL_STORAGE',
POST_NOTIFICATIONS: 'android.permission.POST_NOTIFICATIONS', <- added this
...
WRITE_CALL_LOG: string,
WRITE_CONTACTS: string,
WRITE_EXTERNAL_STORAGE: string,
POST_NOTIFICATIONS: string, <- added this
....

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 the targetSdkVersion to 33 for the permission prompt to show up...

YOU LITERALLY SAVED MY 1 WEEK HEADACHE BRO YOU ARE AWESOME!!!

LalaJumayeva avatar Jun 21 '24 23:06 LalaJumayeva

Instead of PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS);

use PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.POST_NOTIFICATION);

chaudev avatar Jun 27 '24 15:06 chaudev