Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export async function askForNotificationsPermission() {
const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
let finalStatus = existingStatus;
// only ask if permissions have not already been determined, because
// iOS won't necessarily prompt the user a second time.
if (existingStatus !== 'granted') {
// Android remote notification permissions are granted during the app
// install, so this will only ask on iOS
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
// Stop here if the user did not grant permissions
if (finalStatus !== 'granted') {
return null;
}
export async function requestAsync(permission, shouldRequest, redirectReason) {
let status;
if (shouldRequest) {
status = (await Permissions.askAsync(permission)).status;
} else {
status = (await Permissions.getAsync(permission)).status;
}
if (status === 'denied' || (status === 'undetermined' && shouldRequest)) {
// Prompt to open settings and change the permission manually.
// When the user changes the permissions the app will reset so we should
// just return false regardless at this point.
return new Promise(resolve => {
Alert.alert(
'Oh no!',
redirectReason,
[
{
text: 'Nevermind',
onPress: () => resolve(false),
style: 'cancel',
},
{
async componentDidMount() {
// get exisiting locaton permissions first
const { status: existingStatus } = await Permissions.getAsync(
Permissions.LOCATION
);
let finalStatus = existingStatus;
// ask again to grant locaton permissions (if not already allowed)
if (existingStatus !== 'granted') {
const { status } = await Permissions.askAsync(Permissions.LOCATION);
finalStatus = status;
}
// still not allowed to use location?
if (finalStatus !== 'granted') {
return;
}
const { coords } = await Location.getCurrentPositionAsync();
const cameraAccessAsync = async () => {
// get exisiting camera permissions first
const { status: existingStatus } = await Permissions.getAsync(
Permissions.CAMERA
);
let finalStatus = existingStatus;
// ask again to grant camera permissions (if not already allowed)
if (existingStatus !== 'granted') {
const { status } = await Permissions.askAsync(Permissions.CAMERA);
finalStatus = status;
}
return finalStatus === 'granted';
};
_obtainUserFacingNotifPermissionsAsync = async () => {
let permission = await Permissions.getAsync(
Permissions.USER_FACING_NOTIFICATIONS
);
if (permission.status !== 'granted') {
permission = await Permissions.askAsync(
Permissions.USER_FACING_NOTIFICATIONS
);
if (permission.status !== 'granted') {
Alert.alert(`We don't have permission to present notifications.`);
}
}
return permission;
}
export async function shouldShowNotificationPrompt(): Promise {
const settings = await withComputerConfig();
const canPush = await Permissions.getAsync(Permissions.NOTIFICATIONS);
return (
!settings.hasPromptedForNotifications &&
(canPush.status !== Permissions.PermissionStatus.DENIED || canPush.canAskAgain)
);
}
registerForPushNotificationsAsync = async () => {
if (Constants.isDevice) {
const { status: existingStatus } = await Permissions.getAsync(
Permissions.NOTIFICATIONS
);
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Permissions.askAsync(
Permissions.NOTIFICATIONS
);
finalStatus = status;
}
if (finalStatus !== 'granted') {
alert('Failed to get push token for push notification!');
return;
}
let token = await Notifications.getExpoPushTokenAsync();
this._onSetPushToken(token);
} else {
export async function controlledPromptAsync(permission, permissionReason, redirectReason) {
const { status } = await Permissions.getAsync(permission);
if (status === 'denied') {
return requestAsync(permission, false, permissionReason || redirectReason);
} else if (status === 'granted') {
return true;
}
return new Promise(resolve => {
Alert.alert(
`Please Turn On ${PermissionName[permission]}`,
permissionReason,
[
{
text: 'Not Now',
onPress: () => resolve(false),
},
{
_onSaveMoodGraph = async () => {
const { isLoading, imageUri } = this._getProps();
const permission = await Permissions.getAsync(Permissions.CAMERA_ROLL);
if (permission.status !== 'granted') {
const newPermission = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (newPermission.status === 'granted') {
await CameraRoll.saveToCameraRoll(imageUri, 'photo');
}
} else {
await CameraRoll.saveToCameraRoll(imageUri, 'photo');
}
this._cancelSharing();
};