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

Get the current playerid/userid #4

Closed
williamrijksen opened this issue Feb 24, 2016 · 36 comments
Closed

Get the current playerid/userid #4

williamrijksen opened this issue Feb 24, 2016 · 36 comments

Comments

@williamrijksen
Copy link
Contributor

williamrijksen commented Feb 24, 2016

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.

@avishayil
Copy link
Contributor

Good idea.
You now have:

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

on the latest package.

@jvandijk
Copy link

That's an awesome fast response! Thank you!

@avishayil
Copy link
Contributor

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

@williamrijksen
Copy link
Contributor Author

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

@avishayil
Copy link
Contributor

Your'e welcome!
Closing issue.

@alejandrorbradford
Copy link

how does that work for iOS/Swift?

@BeauNouvelle
Copy link

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

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

@nodeEnthu
Copy link

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.

@jpmazza
Copy link

jpmazza commented Jan 11, 2018

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!

@SamiChab
Copy link

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

@bhgames
Copy link

bhgames commented May 14, 2018

Yeah but that new onIds function never gets fired.

@antton
Copy link

antton commented May 15, 2018

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

@sinapcs
Copy link

sinapcs commented May 17, 2018

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

@bhgames
Copy link

bhgames commented May 17, 2018

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.

@sinapcs
Copy link

sinapcs commented May 17, 2018

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

@bhgames
Copy link

bhgames commented May 17, 2018

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

@jorge-mr
Copy link

jorge-mr commented May 23, 2018

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

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

@immortal-88
Copy link

immortal-88 commented May 25, 2018

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

@kenef
Copy link

kenef commented Jun 12, 2018

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.

@davekedar
Copy link

davekedar commented Jun 13, 2018

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

@kenef
Copy link

kenef commented Jun 13, 2018

@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.

@davekedar
Copy link

Thanks @kenef

@pztrick
Copy link

pztrick commented Jun 21, 2018

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

@SirPhemmiey
Copy link

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

@luizmagao
Copy link

How do I get this to work in Cordova?

@kenef
Copy link

kenef commented Aug 3, 2018

@luizmagao what is your environment?

@Mayurk007
Copy link

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

@cyb3rsalih
Copy link

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?

@sourabhCofoundry
Copy link

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

@pawanchhapola
Copy link

This worked for me in functional component:

  const data = await OneSignal.getDeviceState();
  const player_id=data.userId;

@beyzatsc
Copy link

beyzatsc commented Oct 6, 2022

This worked for me in functional component:

  const data = await OneSignal.getDeviceState();
  const player_id=data.userId;

thank you.. you are my hero

@AliAbdullah1122
Copy link

OneSignal

can you send me the code you use here

@AliAbdullah1122
Copy link

i cannot get playerid in latest version v9 of onesignal in react native

@harinderratton
Copy link

OneSignal.getDeviceState().then((idsAvailable) => {console.log('idsAvailable', idsAvailable?.userId)});

@yepes
Copy link

yepes commented Oct 5, 2023

OneSignal.getDeviceState().then(idsAvailable => {
console.log('idsAvailable', idsAvailable?.userId);
});

Throws an "undefined is not a function"

"react-native-onesignal": "^5.0.1",

@nan-li
Copy link
Contributor

nan-li commented Oct 5, 2023

Hi @yepes,

OneSignal.getDeviceState() is no longer available in the latest major release.

Please see this guide for all the methods and properties available in version 5.x.x of the react-native-onesignal library.

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