OneSignal / react-native-onesignal

React Native Library for OneSignal Push Notifications Service
Other
1.57k stars 374 forks source link

[Bug]: permissionChange listener is not giving the correct hasPermission value until background/foreground #1707

Open Chasty opened 4 months ago

Chasty commented 4 months ago

What happened?

At the latest sdk, the listener permissionChange is not giving the correct value in a specific scenario when you enable the permission check from phone settings only when tapping on SETTINGS when notifications not available.

it first gives the value of true, that means the permission check is enabled, but after some milliseconds it returns a false value.

related code.

useEffect(() => {
    const permissionChangedHandler = async (hasPermission: boolean) => {
      console.debug(
        'PermissionsStore.permissionChangedHandler hasPermission?',
        hasPermission,
      );

      if (hasPermission) {
        OneSignal.User.pushSubscription.optIn();
      }
      setIsEnabled(hasPermission);
    };
    OneSignal.Notifications.addEventListener(
      'permissionChange',
      permissionChangedHandler,
    );
  }, []);

const hasPermission = await OneSignal.Notifications.getPermissionAsync();

And then, using the method above to get the permission value is giving all the time a false value until you background/foreground.

https://github.com/OneSignal/react-native-onesignal/assets/18494040/0a45a12c-e2ae-41b5-a100-e958f18b4290

Steps to reproduce?

1. Install react-native-onesignal: 5.1.3
2. Open the app.
3. Register the listener permissionChange.
4. Go to phone settings page to disable the notifications permission check.
5. Go back to the app, the listener gives you a correct value of false for the permission check.
6. Since permision check is disabled, a prompt to go to phone notifications settings appears.
7. Tap on `Settings`
8. Enable the push notifications permission check.
9. Go back to the app, the listener gives you the value of true and after some milliseconds it gives you another value of false which is incorrect.
10. Also when you try to get the `getPermissionAsync` gives you a false value which is wrong.

What did you expect to happen?

1. Install react-native-onesignal: 5.1.3
2. Open the app.
3. Register the listener permissionChange.
4. Go to phone settings page to disable the notifications permission check.
5. Go back to the app, the listener gives you a correct value of false for the permission check.
6. Since permision check is disabled, a prompt to go to phone notifications settings appears.
7. Tap on `Settings`
8. Enable the push notifications permission check.
9. Go back to the app, the listener should give you the value of true.
10. Also when you try to get the `getPermissionAsync` should give you the value of true.

React Native OneSignal SDK version

5.1.3

Which platform(s) are affected?

Relevant log output

No output.

Code of Conduct

Chasty commented 4 months ago

If you want to reproduce it, try to run on a physical android phone, and also I've created the repository, so u can take a look there.

https://github.com/Chasty/onesignal-issue

Chasty commented 3 months ago

@nan-li Hello, could u able to reproduce this?

Nanjae commented 3 months ago

I've got similar issue on Android

App request notification permission(first time) through android native request permission modal and accept it, then OneSignal.Notifications.addEventListener("permissionChange") not detect permissionChange.

After app went to background and came back foreground, AppState.addEventListener('change') call getPermissionAsync and it response true. In same time, OneSignal.Notifications.addEventListener("permissionChange") finally detect permissionChange.

React Native version : 0.73 React Native OneSignal SDK version : 5.2.0