I execute following code in my useEffect hook:
const requestNotificationPermission = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS,
{
title: 'Notification Permission',
message: 'Allow the app to send you notifications',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('Notification permission granted');
} else {
console.log('Notification permission denied');
}
} catch (err) {
console.warn(err);
}
};
useEffect(() => {
AppState.addEventListener('change', (state) => {
if (state === 'active') {
fetchData();
}
});
requestNotificationPermission();
fetchData();
getList();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
But it acts really weird. Without asking for the permission it works perfectly fine, but when using it the fetchData() function execute multiple times. And also the permission gets automatically denied without a popup or any ask of the permission. Then my app get's bugged by displaying an array multiple times even when I only want do display once.
I don't know what the problem is and also looked up in the internet, but no one seems to have this problem. If I try to send a push notification via react native using PushNotification.localNotification it works, but I honestly don't know if this has something to do with my problem. I also included in my AndroidManifes.xml and my API version is 31 or higher. So maybe someone with knowledge could help me
Actually the push notification permission lie in the normal category permission like Internet permission, not in dangerous category permission.
You don't have to ask for push notification permissions.
While Contacts/Locations are the dangerous permissions because you are accessing user data. So it is always needed to ask the user to allow it.
As Answered here you don't need permissions for push notifications. also refer this
Android 13 (API level 33) and higher supports a runtime permission for sending non-exempt (including Foreground Services (FGS)) notifications from an app: POST_NOTIFICATIONS. This change helps users focus on the notifications that are most important to them.
AndroidManifest.xml
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> //<--
<application ...>
...
</application>
trigger using this:
pushNotificationPermissionLauncher.launch(android.Manifest.permission.POST_NOTIFICATIONS)
Result Handling
private val pushNotificationPermissionLauncher = registerForActivityResult(RequestPermission()) { granted ->
viewModel.inputs.onTurnOnNotificationsClicked(granted)
}