0

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

JUNSKI
  • 17
  • 4

1 Answers1

0

For Android <=12

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


For Android 13 & higher

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)
        }
Kishan
  • 106
  • 6
  • If I use api 33 but the phone is on Android 12 should I still implement it for Android 13 phones? – JUNSKI Mar 25 '23 at 11:59
  • Maybe should I just ask for this permission when the device the user is using is running on Android 13? – JUNSKI Mar 25 '23 at 12:14
  • already answered if the android is less or equal to version 12 then no permission should be required and it won't give any errror if you add permissions – Kishan Mar 27 '23 at 04:24