javascript #reactnative #mobile #development
I am developing an application in react native. I allow notifications. but when I take the application down from the simulator and re-enter it or reload it again with the 'r' command in the terminal, it asks for permission on the same page.
Here are my codes
`
const checkAndRequestNotificationPermission = async () => {
try {
const storedPermission = await AsyncStorage.getItem('notificationPermission');
if (storedPermission === 'granted') {
return;
}
const { status: existingStatus } = await Notifications.getPermissionsAsync();
if (existingStatus === 'granted') {
await AsyncStorage.setItem('notificationPermission', 'granted');
return;
}
if (Platform.OS === 'ios') {
Alert.alert(
promotion.t('notificationTitle'),
promotion.t('notificationMessage'),
[
{
text: promotion.t('notificationCancel'),
style: "cancel",
onPress: async () => {
await AsyncStorage.setItem('notificationPermission', 'denied');
}
},
{
text: promotion.t('notificationAllow'),
onPress: async () => {
const { status } = await Notifications.requestPermissionsAsync();
await AsyncStorage.setItem('notificationPermission', status);
}
}
]
);
} else {
const { status } = await Notifications.requestPermissionsAsync();
await AsyncStorage.setItem('notificationPermission', status);
}
} catch (error) {
console.log('Error requesting notification permission:', error);
}
};
SOCIAL SHARE CARD GENERATOR