OneSignal / react-native-onesignal

React Native Library for OneSignal Push Notifications Service
Other
1.56k stars 371 forks source link

[Bug]: The requestPermission method for notifications returns a Promise<array> on iOS. #1655

Open WooMinGy opened 5 months ago

WooMinGy commented 5 months ago

What happened?

react-native: 0.73.2 device: iphone 12 mini iOS version: 17.3.1 onesignal version: 5.0.5

I implemented actions to change based on whether the return value of requestPermission is true or false. However, the actions did not occur as specified on iOS. Upon checking, I found that the returned value was coming in as an array.

Below is a photo of my code and the result values confirmed via console.log.

  const requestPermission = useCallback(async () => {
    const isSucceed = await OneSignal.Notifications.requestPermission(true);

    if (Platform.OS === 'ios') {
      console.log('isSucceed: ', isSucceed);
      console.log(`
        type: ${typeof isSucceed}
        isArray: ${isArray(isSucceed)}
      `);

      return isArray(isSucceed) ? isSucceed[0] : isSucceed;
    }

    return isSucceed;
  }, []);

image

Steps to reproduce?

1. Install v5.0.5 
2. Launch the app on iOS device
3. Use the requestPermission method for notifications with console.log like my code

What did you expect to happen?

image

It should have returned a Promise as the specified type. On Android, it correctly returns a boolean, but on iOS, it is returning a Promise<boolean[]> instead.

React Native OneSignal SDK version

Release 5.0.5

Which platform(s) are affected?

Relevant log output

No response

Code of Conduct

nan-li commented 5 months ago

Hi @WooMinGy, thank you for reporting this bug. We will make the fix to return boolean as expected on iOS.

WooMinGy commented 5 months ago

@nan-li , thank you for checking the reporting quickly. Please let me know after it has been fixed.

ChromeQ commented 3 months ago

I am also falling into this issue, any updates since Feb @nan-li?

The workaround in the meantime is to override the types and unwrap the array:

// FIXME: iOS returns a boolean tuple so unwrap it to provide a boolean as expected
// @see https://github.com/OneSignal/react-native-onesignal/issues/1655
const value = (await OneSignal.Notifications.requestPermission(true)) as boolean | [boolean];
const granted = Array.isArray(value) ? value[0] : value;

// `granted` is now always a boolean as per the types