Permissions.askAsync(Permissions.NOTIFICATIONS) not working React-native

Viewed 540

I am new to React-native. dependencies are mentioned below

 "dependencies": {
    "@react-native-community/masked-view": "0.1.6",
    "@react-navigation/drawer": "^5.8.2",
    "@react-navigation/native": "^5.5.1",
    "@react-navigation/stack": "^5.2.10",
    "axios": "^0.19.2",
    "expo": "37.0.8",
    "expo-notifications": "^0.1.1",
    "expo-permissions": "^8.1.0",
    "expo-secure-store": "^8.1.0",
    "ngrok": "^3.2.7",
    "react": "~16.9.0",
    "react-dom": "~16.9.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-37.0.1.tar.gz",
    "react-native-animatable": "^1.3.3",
    "react-native-datepicker": "^1.7.2",
    "react-native-elements": "^2.0.2",
    "react-native-gesture-handler": "^1.6.1",
    "react-native-reanimated": "~1.7.0",
    "react-native-safe-area-context": "0.7.3",
    "react-native-safe-area-view": "^1.1.1",
    "react-native-screens": "~2.2.0",
    "react-native-swipeout": "^2.3.6",
    "react-native-vector-icons": "^6.6.0",
    "react-native-web": "~0.11.7",
    "react-redux": "^7.2.0",
    "redux": "^4.0.5",
    "redux-logger": "^3.0.6",
    "redux-persist": "^6.0.0",
    "redux-thunk": "^2.3.0"
  }

this is the part where I need to check the permissions and if no permission then I need to ask for it.

import * as Permissions from "expo-permissions"

import { Notifications} from 'expo'

obtainNotificationPermission =  async () => {

    const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS)
   
    if( existingStatus !== 'granted'){
        const nextpermission = await Permissions.askAsync(Permissions.NOTIFICATIONS)
        
        if(nextpermission.status !=='granted'){
            Alert.alert('Permission not granted to show notifications')
        }
    }
   
}

presentLocalNotification = async (date) =>{
   await this.obtainNotificationPermission();
    Notifications.presentLocalNotificationAsync({title:'test',body: 'Reservation for'+date+' 
    requested',
    android:{
        sound: true,
        vibrate: true
        }
    })    
}

If I manually allow permission, I am getting the notification. but if I didn't give expo the notification permission and try running the application, It's not even asking for the permission and the alert is showing. Any idea where it went wrong?

1 Answers

We've been struggling with this issue as it makes apps hard to test. It's not an expo/react native issue per-se, but rather it's a feature of android.

Android remembers the permission even between installations. So if you install once, and ask for permission, then uninstall the app. The subsequent attempts to get the permission dialog to appear will not work.

We had to test on different devices to get it working.

Related