Notes
Handling permissions in Expo
checking and asking for permissions in Expo with getAsync and askAsync, the permission types, and why to check for granted
Expo
2 minutes
1expo install expo-permissions
- You can check if a permission is granted with
Permissions.getAsync(...permissionTypes)and ask for a permission withPermissions.askAsync(...types) - Both return a
Promisewith hasstatusandexpiresas well as details about scope - The
statusis usuallygrantedordenied. It could also beundeterminedin case ofPermissions.NOTIFICATIONSfor iOS, as iOS does not disambiguateundeterminedfromdenied - Make sure you’re basing your logic on
grantedand!grantedto account for cases likeundeterminedthat only happen on iOS and only for some permissions - on iOS remote notifications only show if
allowsSound,allowsAlertorallowsBadgeis 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}