React-native-onesignal: Get the current playerid/userid

Created on 24 Feb 2016  ·  29Comments  ·  Source: OneSignal/react-native-onesignal

Is it currently possible to get the current OneSignal userid/playerid like this on iOS? https://documentation.onesignal.com/docs/ios-sdk-api#section-idsavailable.

I need it to send a specific notification to a specific user.

Most helpful comment

The goal was to recieve PlayerID from Onesignal on application launch.
So in App.js for me worked the following code:

let userID;
componentWillMount() {
    OneSignal.init(ONESIGNAL_APP_ID, {
      kOSSettingsKeyAutoPrompt: true,
    });
    OneSignal.getPermissionSubscriptionState( (status) => {
      userID = status.userId;
      alert(userID);
    });
  }

All 29 comments

Good idea.
You now have:

OneSignal.idsAvailable((idsAvailable) => { 
    console.log(idsAvailable.playerId);
    console.log(idsAvailable.pushToken);
});

on the latest package.

That's an awesome fast response! Thank you!

Let me know if it works or you have any issues.
If not, i'll close this issue...
Thanks!

@avishayil I looks like it's working on iOS. Thank you!

Your'e welcome!
Closing issue.

how does that work for iOS/Swift?

@alejandrorbradford and anyone else getting here after a google search here it is for Swift:

OneSignal.idsAvailable { (pushID, pushToken) in
    print(pushID)
    print(pushToken)
}

for people coming here in 2017:

Deprecations and Major Changes
March 21, 2017: Removed getRegistrationId() from documentation. getUserId() should always be used instead.
March 15, 2016: Deprecated getIdsAvailable() in favor of getUserId() and getRegistrationId(). Deprecated idsAvailableCallback in favor of subscriptionChange event.

I cannot get the playerId.
I tried with OneSignal.idsAvailable but is not a function. Also tried with OneSignal.getUserId and it display the error "is not a function".

How can I get the player id? I have installed the version 3.0.7.

Thanks in advance!

Apparently it has now changed. According to the docs you have to add an event listener:
````
componentWillMount() {
OneSignal.addEventListener('ids', this.onIds);
}

componentWillUnmount() {
OneSignal.removeEventListener('ids', this.onIds);
}

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

Yeah but that new onIds function never gets fired.

What about Web push? Is it possible to "send notifications to an specific user"?

@bhgames @jpmazza could you find a way to get current playerID?

So, using the latest version (3.2.2)

componentWillMount() {
OneSignal.init(Config().pushId, {kOSSettingsKeyAutoPrompt: true});

OneSignal.addEventListener('ids', this.updatePushId.bind(this));
OneSignal.configure()

}

async updatePushId(device) {
postJSON(UPDATE_PUSHID_URL,
{
user_id: this.props.user.id,
push_id: device.userId
},
{ 'Authorization': this.props.authentication_token }
);
};

This worked for me.

@bhgames it worked. thanks.
OneSignal.configure() did the trick. why there is nothing about it in documents :|

Because you needed to know it. That's why. :P React Native development is the worst development.

https://documentation.onesignal.com/docs/ios-native-sdk#section--getpermissionsubscriptionstate-

let userId = OneSignal.getPermissionSubscriptionState().subscriptionStatus.userId
print("your userID:",userId!)

The goal was to recieve PlayerID from Onesignal on application launch.
So in App.js for me worked the following code:

let userID;
componentWillMount() {
    OneSignal.init(ONESIGNAL_APP_ID, {
      kOSSettingsKeyAutoPrompt: true,
    });
    OneSignal.getPermissionSubscriptionState( (status) => {
      userID = status.userId;
      alert(userID);
    });
  }

Has anyone figured this out? How can I get the Player ID of a specific device? I need this to be able to send push notifications to my users. Groups and filters are worthless in my application. The only thing I need is the Player ID and I can't find anything about how to get this in any of the documentation. Can someone please help? My app is built in Ionic V3.

I have the same problem, documentatoin is not clear

import React from "react";
import { AsyncStorage } from "react-native";
import { createRootNavigator } from "./router";
import { isSignedIn } from "./auth";
import { YellowBox } from "react-native";
import OneSignal from "react-native-onesignal"; // Import package from node modules
//import { Provider } from 'react-redux';
//import { store } from '../app/redux/app-reducer';
YellowBox.ignoreWarnings([
  "Warning: isMounted(...) is deprecated",
  "Module RCTImageLoader"
]);
export default class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      signedIn: false,
      checkedSignIn: false
    };
  }
  componentWillMount() {
    OneSignal.init("my app id");

    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("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);
  }

  componentDidMount() {
    // status = AsyncStorage.getItem('loggedIn');
    // if(status === 'success'){
    //   this.setState({checkedSignIn: true});
    // }
    isSignedIn()
      .then(res => this.setState({ signedIn: res, checkedSignIn: true }))
      .catch(err => alert("An error occurred"));
  }

  render() {
    const { checkedSignIn, signedIn } = this.state;

    // If we haven't checked AsyncStorage yet, don't render anything (better ways to do this)
    if (!checkedSignIn) {
      return null;
    }

    const Layout = createRootNavigator(signedIn);
    return (
      //<Provider store={store}>
      <Layout />
      //  </Provider>
    );
  }
}

this is my code and it was told to me in other thread that i should use OneSignal.getPermissionSubscriptionState(callback) but where ? and how ? and how will I pass playerID? please someone help

@davekedar I was able to get this to work by calling it in my ionViewDidLoad()

window["plugins"].OneSignal.getIds(function(ids) {
    //alert(JSON.stringify(ids['userId']));
    let playerID = ids['userId'];
    //alert(playerID);
});

This is working for me; give it a shot.

Thanks @kenef

Just invoking OneSignal.configure() was enough to have an event emitted on the listener 'ids'.

E.g.:

  componentWillMount() {
      OneSignal.init("APP ID GOES HERE", {kOSSettingsKeyAutoPrompt : true});
      OneSignal.addEventListener('received', this.onReceived);
      OneSignal.addEventListener('opened', this.onOpened);
      OneSignal.addEventListener('ids', this.onIds);

      OneSignal.configure();  // <-- add this line
  }

  componentWillUnmount() {
      OneSignal.removeEventListener('received', this.onReceived);
      OneSignal.removeEventListener('opened', this.onOpened);
      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);
  }

To help those still having issues getting the player_id.

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

The device returns an object. So to really get the player_id you're looking for, you should do something like device.userId

How do I get this to work in Cordova?

@luizmagao what is your environment?

hello i have quickblox dialogid how i fetch quickblox userid by using this dialogid

Just invoking OneSignal.configure() was enough to have an event emitted on the listener 'ids'.

E.g.:

  componentWillMount() {
      OneSignal.init("APP ID GOES HERE", {kOSSettingsKeyAutoPrompt : true});
      OneSignal.addEventListener('received', this.onReceived);
      OneSignal.addEventListener('opened', this.onOpened);
      OneSignal.addEventListener('ids', this.onIds);

      OneSignal.configure();  // <-- add this line
  }

  componentWillUnmount() {
      OneSignal.removeEventListener('received', this.onReceived);
      OneSignal.removeEventListener('opened', this.onOpened);
      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);
  }

So, How can you extract the ID ? Could you explain it more?

Get the all playerid/userid using jquery and other .Please let me know it's possible or not

Was this page helpful?
0 / 5 - 0 ratings