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;
}
// Get the token that uniquely identifies this device
let token = await Notifications.getExpoPushTokenAsync();
return token;
}
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 {
// alert('Must use physical device for Push Notifications');
}
await scheduleMoodReminders();
};
import Constants from 'expo-constants';
import * as IntentLauncher from 'expo-intent-launcher';
import * as Permissions from 'expo-permissions';
import { Alert, Linking, Platform } from 'react-native';
const PermissionName = {
[Permissions.CAMERA]: 'The Camera',
[Permissions.LOCATION]: 'GPS',
[Permissions.CAMERA_ROLL]: 'The Gallery',
[Permissions.AUDIO_RECORDING]: 'The Microphone',
[Permissions.NOTIFICATIONS]: 'Push Notifications',
[Permissions.USER_FACING_NOTIFICATIONS]: 'Notifications',
[Permissions.CONTACTS]: 'Your Contacts',
[Permissions.CALENDAR]: 'Calendar',
[Permissions.REMINDERS]: 'Reminders',
};
// Use a controlled prompt to ensure you retain access to the permission prompt.
// If the user rejects the controlled prompt, you can always prompt them again when they're ready.
// Otherwise you'll need to redirect the user to the system settings.
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;
}
const onAccept = async () => {
setProcessing(true);
await Permissions.askAsync(Permissions.NOTIFICATIONS);
await withComputerConfig(x => {
x.hasPromptedForNotifications = true;
x.readyCheckNotificationsEnabled = true;
x.gameStartNotificationsEnabled = true;
});
await updateNotificationSubscriptions();
setProcessing(false);
onClose();
};
initnotify: async ()=>{
const {status} = await Permissions.askAsync(Permissions.NOTIFICATIONS);
if (status != "granted") {
alert('you need to enable notification permission in settings')
return false;
} else {
Notifications.createChannelAndroidAsync('default', {
name: 'Default',
sound: true,
});
Notifications.createChannelAndroidAsync('reminders', {
name: 'Reminders',
priority: 'max',
vibrate: [0, 250, 250, 250],
sound: true,
});
Notifications.createChannelAndroidAsync('chat-messages', {
name: 'Chat messages',
useEffect(async () => {
await Permissions.askAsync(Permissions.NOTIFICATIONS);
connectionInfo();
setUrlEventListener();
if (Platform.OS === OS_TYPES.ANDROID) {
Notifications.createChannelAndroidAsync(DEFAULT, {
name: NOTIFICATION,
sound: true
});
}
Notifications.addListener(handleNotification);
}, []);
_obtainRemoteNotifPermissionsAsync = async () => {
let permission = await Permissions.getAsync(Permissions.NOTIFICATIONS);
if (permission.status !== 'granted') {
permission = await Permissions.askAsync(Permissions.NOTIFICATIONS);
if (permission.status !== 'granted') {
Alert.alert(
`We don't have permission to receive remote notifications.`
);
}
}
return permission;
}
onPress={() =>
this.invokePermissionsFunction(
...[
Permissions.CAMERA,
Permissions.AUDIO_RECORDING,
Permissions.LOCATION,
Permissions.USER_FACING_NOTIFICATIONS,
Permissions.NOTIFICATIONS,
Permissions.CONTACTS,
Permissions.SYSTEM_BRIGHTNESS,
Permissions.CAMERA_ROLL,
Permissions.CALENDAR,
Permissions.REMINDERS,
]
)
}