Can't get the userID(PlayerID). It's undefinded (ReactNative, OneSignal)

Viewed 313

Via my application I will send notification throught OneSignal. But I can't get userID from OneSignal. I read this userID from GET request and save it in DB. After I send notification via PHP.

How I can get this userID If I always get undefined?

export default class App extends Component {

  constructor(props) {
    super(props);
    this.WEBVIEW_REF = React.createRef();
  }

  componentDidMount() {
    BackHandler.addEventListener('hardwareBackPress', this.handleBackButton);
    OneSignal.setLogLevel(6, 0);
    OneSignal.setAppId("fdb89158-4072-4964-b490-6ba70fb6b5fd");

    OneSignal.promptForPushNotificationsWithUserResponse(response => {
    });

    OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent => {
      let notification = notificationReceivedEvent.getNotification();
      console.log("notification: ", notification);
      const data = notification.additionalData
      console.log("additionalData: ", notification.additionalData);
      notificationReceivedEvent.complete(notification);
    });

    OneSignal.setNotificationOpenedHandler(notification => {
    });

    OneSignal.addPermissionObserver(event => {
      console.log("OneSignal: permission changed:", event);
  });

  OneSignal.addSubscriptionObserver(event => {
    console.log("OneSignal: subscription changed to userId:", event.to.userId);
  });
  }

  componentWillUnmount() {
    BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton);
  }

  handleBackButton = ()=>{
    this.WEBVIEW_REF.current.goBack();
    return true;
  }

  onNavigationStateChange(navState) {
    this.setState({
      canGoBack: navState.canGoBack
    });
  }
  render() {
    const deviceState = OneSignal.getDeviceState();
    return (
        <WebView
        source={{ uri: 'https://www.google.com/search?q='+ deviceState.userId}}
        style={{ marginTop: 35 }}
        ref={this.WEBVIEW_REF}
        onNavigationStateChange={this.onNavigationStateChange.bind(this)}
      />
    );
}
}
1 Answers

getDeviceState returns a asynchronous so make sure you're awaiting on it.

Related