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

OneSignal.getDeviceState() always undefined in android #1241

Closed
weiloon1234 opened this issue Jun 4, 2021 · 3 comments
Closed

OneSignal.getDeviceState() always undefined in android #1241

weiloon1234 opened this issue Jun 4, 2021 · 3 comments

Comments

@weiloon1234
Copy link

weiloon1234 commented Jun 4, 2021

Description:
await OneSignal.getDeviceState() always undefined in android

Environment
react: 17.0.1
react-native: 0.64.1
react-native-onesignal: 4.1.1

without expo
added react-native-onesignal with npm

Steps to Reproduce Issue:

  1. install the one signal SDK using npm
  2. follow the guide https://documentation.onesignal.com/docs/react-native-sdk-setup to setup
  3. iOS, it prompt for notification permission, iOS response correctly
  4. android, tried physical device with latest android, also tried android 8 with emulator, both undefined OneSignal.getDeviceState()
const MainNavigator = () => {
  const initOneSignal = async () => {
    OneSignal.setAppId(Config.ONE_SIGNAL_APP_ID)
    OneSignal.setLogLevel(6, 0)
    OneSignal.setRequiresUserPrivacyConsent(false)
    OneSignal.promptForPushNotificationsWithUserResponse(response => {
      console.log('Prompt response:', response)
    })

    try {
      console.log('THE RESPONSE AS BELOW')
      const deviceState = await OneSignal.getDeviceState()
      // const { userId } = await OneSignal.getDeviceState()
      // console.log(userId)
      console.log(deviceState)
    } catch (e) {
      console.log(e)
    }
  }
  useEffect(() => {
    initOneSignal()
    startWatching()

    return () => {
      stopWatching()
    }
  }, [])
}

export default MainNavigator

the console.log for android
image

iOS response
image

@rgomezp
Copy link
Contributor

rgomezp commented Jul 8, 2021

Howdy,
I would recommend putting the getDeviceState call in a subscription observer. I'm not sure what your use case is for it, but it's possible that the device state is firing before the subscription fully completes.

Notice the warning we give in the documentation regarding this being a "snapshot" of the user state at that time. By putting it in the observer, you can be sure that the subscription has changed before trying to get the user state for it.

Note: this is similar to this issue. This could provide some potentially useful information as well.

One more thing, open up your Android studio to see if the logcat prints any other messages not shown in the RN logger

@rgomezp
Copy link
Contributor

rgomezp commented Aug 10, 2021

Closing due to inactivity.

@rgomezp rgomezp closed this as completed Aug 10, 2021
@guillaumetran
Copy link

Still not fixed

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

No branches or pull requests

3 participants