Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Notifications.hasPermission() does not always return a boolean on iOS #1555

Closed
2 of 3 tasks
Bjarkinn opened this issue Sep 13, 2023 · 9 comments
Closed
2 of 3 tasks
Labels

Comments

@Bjarkinn
Copy link

Bjarkinn commented Sep 13, 2023

What happened?

If the notification permission is checked after the permission is manually changed by the user in the settings the Notifications.hasPermission() function returns the following object {permission: boolean} instead of boolean

Video

OneSignal_iOS_bug.MOV

In the video The following code was used that calls Notifications.hasPermission() every time the app comes into foreground

const OneSignalIosBug: FC = () => {
  const [hasNotificationPermission, setHasNotificationPermission] = useState(
    OneSignal.Notifications.hasPermission()
  )

  useAppStateListener({
    onForeground: () => {
      const currNotificationPermission = OneSignal.Notifications.hasPermission()
      setHasNotificationPermission(currNotificationPermission)
    },
  })

  return (
    <Text style={styles.oneSignalPermissionText}>
      {(isAndroid ? 'Android: ' : 'iOS: ') +
        JSON.stringify(hasNotificationPermission)}
    </Text>
  )
}

const styles = StyleSheet.create({
  oneSignalPermissionText: {
    ...StyleSheet.absoluteFillObject,
    textAlign: 'center',
    top: '50%',
  },
})

export default React.memo(OneSignalIosBug)

Steps to reproduce?

1. Installed packages

"react": "18.2.0",
"react-native": "0.72.4",
"expo": "49.0.9",
"react-native-onesignal": "5.0.0",

2. Check the permission with `OneSignal.Notifications.hasPermission()` that returns a boolean
3. Go to the iOS app setting and change the notification permission manually
4. Check the permission again with `OneSignal.Notifications.hasPermission()` that returns an object {permission: boolean}

What did you expect to happen?

I expected Notifications.hasPermission() to always return a boolean

React Native OneSignal SDK version

5.0.0

Which platform(s) are affected?

  • iOS
  • Android

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@jennantilla
Copy link
Contributor

Hello @Bjarkinn thank you for flagging! We'll work on a fix for this and let you know when it's released!

@ChromeQ
Copy link

ChromeQ commented Sep 18, 2023

On a related note there is a bug reported here also related to hasPermission #1506 (comment)

Perhaps these can be investigated together. fyi this one also affects android not just ios

@ChromeQ
Copy link

ChromeQ commented Sep 18, 2023

@Bjarkinn @jennantilla I can also confirm this is also happening on Android, not just iOS. And not just the hasPermission method, I'm using the permissionChange event listener:

const handler = (granted: boolean) => {
  console.log('PERM CHANGE HANDLER', granted); // PERM CHANGE HANDLER {"permission": true}
};

OneSignal.Notifications.addEventListener('permissionChange', handler);

This is fine as I can extract the permission, maybe it is just the types need updating:
image

@theonetheycallneo
Copy link

We did this for now:

      const hasPermission = await OneSignal.Notifications.requestPermission(true)
      //TODO: hasPermission is actually an object {permission: boolean}, bug on one signal.
      //@ts-ignore
      const isPermissionEnabled = hasPermission?.permission ?? hasPermission ?? false
      store.permissions.setIsPushDisabled(!isPermissionEnabled)

@jennantilla
Copy link
Contributor

Hello everyone! Thank you for your patience.

A fix for this is available in our latest release, 5.0.1. Please let us know if you have any additional questions or concerns!

@Bjarkinn
Copy link
Author

Bjarkinn commented Oct 9, 2023

Looks like everything behaves like it should on 5.0.1
Good job 🤝

@Salmankhan033
Copy link

Same issue on V5.0.4 @jennantilla

@rakshitbharat
Copy link

This issue should be reopened because there is a new issue occurred in which the Boolean itself started returning false always.

@ChromeQ
Copy link

ChromeQ commented Feb 1, 2024

No this issue should not be reopened as you say:

there is a new issue occurred

It is a new issue, so please open a new issue instead of commenting on old ones.
The fix from this issue was released as v5.0.1 in this PR - the relevant change there in src/events/EventManager.ts has not changed since v5.0.1 through to latest v5.0.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants