Notes

Handling permissions in Expo

Edit on GitHub

Expo
2 minutes
1expo install expo-permissions
  • You can check if a permission is granted with Permissions.getAsync(...permissionTypes) and ask for a permission with Permissions.askAsync(...types)
  • Both return a Promise with has status and expires as well as details about scope
  • The status is usually granted or denied. It could also be undetermined in case of Permissions.NOTIFICATIONS for iOS, as iOS does not disambiguate undetermined from denied
  • Make sure you’re basing your logic on granted and !granted to account for cases like undetermined that only happen on iOS and only for some permissions
  • on iOS remote notifications only show if allowsSound, allowsAlert or allowsBadge is set.
  • There are about a dozen permission types

    • NOTIFICATIONS
    • USER_FACING_NOTIFICATIONS
    • LOCATION
    • CAMERA
    • AUDIO_RECORDING
    • CONTACTS
    • CAMERA_ROLL
    • CALENDAR
    • REMINDERS
    • SYSTEM_BRIGHTNESS
    • MOTION
1async function alertIfRemoteNotificationsDisabledAsync() {
2  const { status } = await Permissions.getAsync(Permissions.NOTIFICATIONS)
3  if (status !== 'granted') {
4    alert('Hey! You might want to enable notifications for my app, they are good.')
5  }
6}
1async function checkMultiPermissions() {
2  const { status, expires, permissions } = await Permissions.getAsync(Permissions.CALENDAR, Permissions.CONTACTS)
3  if (status !== 'granted') {
4    alert('Hey! You heve not enabled selected permissions')
5  }
6}

Here’s an example asking for permissions for push notifications..

 1const askPermissions = async () => {
 2  // See if app already has permission
 3  const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS)
 4  let finalStatus = existingStatus
 5
 6  // No existing permisson, ask for it
 7  if (existingStatus !== 'granted') {
 8    const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS)
 9    finalStatus = status
10  }
11
12  // Permission denied
13  if (finalStatus !== 'granted') {
14    return false
15  }
16
17  return true
18}