'How to get player ID using one signal in React Native?

i'm trying to use oneSignal. I follow the steps from the documentation. when I run the app, it should console the device information according to this code in my index.js :

constructor(properties) {
    super(properties);
    // OneSignal.init("e353b33e-8b5f-4093-8a86-073b0504b5f2");

    OneSignal.addEventListener('received', this.onReceived);
    OneSignal.addEventListener('opened', this.onOpened);
    OneSignal.addEventListener('ids', this.onIds);
  }

componentWillUnmount() {
    OneSignal.removeEventListener('received', this.onReceived);
    OneSignal.removeEventListener('opened', this.onOpened);
    // OneSignal.removeEventListener('registered', this.onRegistered);
    OneSignal.removeEventListener('ids', this.onIds);
}

onReceived(notification) {
    console.log("Notification received: ", notification);
}

onOpened(openResult) {
  console.log('Message: ', openResult.notification.payload.body);
  console.log('Data: ', openResult.notification.payload.additionalData);
  console.log('isActive: ', openResult.notification.isAppInFocus);
  console.log('openResult: ', openResult);
}

onIds(device) {
    console.log('Device info: ', device);
}

when I open the console, it didn't show the device information from the function onIds().

can someone tell me what i'm doing wrong? i'm new to this so thanks for your help



Solution 1:[1]

I've just encountered this issue and it's quite unclear but if you check the docs they say:

Please note that calling OneSignal.configure() causes the ids event to fire.

I just added OneSignal.configure() at the end of the constructor and now it triggers onIds().

Solution 2:[2]

This worked for me in functional component:

const data = await OneSignal.getDeviceState();

const player_id=data.userId;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Charles K
Solution 2 Pawan Verma