1expo install expo-permissions
Permissions.getAsync(...permissionTypes)
and ask for a permission with Permissions.askAsync(...types)
Promise
with has status
and expires
as well as details about scopestatus
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
granted
and !granted
to account for cases like undetermined
that only happen on iOS and only for some permissionsallowsSound
, allowsAlert
or allowsBadge
is set.There are about a dozen permission types
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}