facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
117.18k stars 24.09k forks source link

permission is null in react native app #34409

Open nivas412 opened 1 year ago

nivas412 commented 1 year ago

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
    ]);
JosuGZ commented 1 year ago

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

suyashvash commented 1 year ago

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

JosuGZ commented 1 year ago

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.

sideshowbob commented 1 year ago

Any news or workaround on this one?

federicomiralles commented 1 year ago

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:

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...

jedsada-jed commented 1 year ago

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);
Gautham495 commented 1 year ago

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 !!!!!!!!!

amitasort123 commented 1 year ago

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...

stanislav-sidorov-empeek commented 1 year ago

react-native must be 0.70+

Prashant-Verma-5c commented 1 year ago

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

hamza4213 commented 11 months ago

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.

umairm1alik commented 11 months ago

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); }

jaaywags commented 10 months ago

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.

HoanNguyen135 commented 10 months ago

Thank bro. It 's worked

Vigneshwaran-crypto commented 8 months ago

@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);
  });`
mawais78 commented 7 months ago

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');
    }
  }
)
Robiullah2244 commented 6 months ago

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'>,
manuelabarca commented 5 months ago

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); } }

Aleksa-startify commented 4 months ago

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.

brunoosella commented 3 months ago

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?

LalaJumayeva commented 3 weeks ago

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!!!

chaudev commented 2 weeks ago

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

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