Giter Site home page Giter Site logo

Comments (36)

immortal-88 avatar immortal-88 commented on May 23, 2024 22

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

from react-native-onesignal.

bhgames avatar bhgames commented on May 23, 2024 18

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

from react-native-onesignal.

pztrick avatar pztrick commented on May 23, 2024 17

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

from react-native-onesignal.

avishayil avatar avishayil commented on May 23, 2024 13

Good idea.
You now have:

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

on the latest package.

from react-native-onesignal.

BeauNouvelle avatar BeauNouvelle commented on May 23, 2024 7

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

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

from react-native-onesignal.

bhgames avatar bhgames commented on May 23, 2024 7

Yeah but that new onIds function never gets fired.

from react-native-onesignal.

bhgames avatar bhgames commented on May 23, 2024 7

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.

from react-native-onesignal.

pawanchhapola avatar pawanchhapola commented on May 23, 2024 6

This worked for me in functional component:

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

from react-native-onesignal.

harinderratton avatar harinderratton commented on May 23, 2024 6

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

from react-native-onesignal.

jpmazza avatar jpmazza commented on May 23, 2024 4

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!

from react-native-onesignal.

sinapcs avatar sinapcs commented on May 23, 2024 4

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

from react-native-onesignal.

nodeEnthu avatar nodeEnthu commented on May 23, 2024 3

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.

from react-native-onesignal.

jorge-mr avatar jorge-mr commented on May 23, 2024 2

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

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

from react-native-onesignal.

kenef avatar kenef commented on May 23, 2024 2

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.

from react-native-onesignal.

davekedar avatar davekedar commented on May 23, 2024 1

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

from react-native-onesignal.

kenef avatar kenef commented on May 23, 2024 1

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

from react-native-onesignal.

davekedar avatar davekedar commented on May 23, 2024 1

Thanks @kenef

from react-native-onesignal.

jvandijk avatar jvandijk commented on May 23, 2024

That's an awesome fast response! Thank you!

from react-native-onesignal.

avishayil avatar avishayil commented on May 23, 2024

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

from react-native-onesignal.

williamrijksen avatar williamrijksen commented on May 23, 2024

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

from react-native-onesignal.

avishayil avatar avishayil commented on May 23, 2024

Your'e welcome!
Closing issue.

from react-native-onesignal.

alejandrorbradford avatar alejandrorbradford commented on May 23, 2024

how does that work for iOS/Swift?

from react-native-onesignal.

SamiChab avatar SamiChab commented on May 23, 2024

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

from react-native-onesignal.

antton avatar antton commented on May 23, 2024

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

from react-native-onesignal.

sinapcs avatar sinapcs commented on May 23, 2024

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

from react-native-onesignal.

SirPhemmiey avatar SirPhemmiey commented on May 23, 2024

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

from react-native-onesignal.

luizmagao avatar luizmagao commented on May 23, 2024

How do I get this to work in Cordova?

from react-native-onesignal.

kenef avatar kenef commented on May 23, 2024

@luizmagao what is your environment?

from react-native-onesignal.

Mayurk007 avatar Mayurk007 commented on May 23, 2024

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

from react-native-onesignal.

cyb3rsalih avatar cyb3rsalih commented on May 23, 2024

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?

from react-native-onesignal.

sourabhCofoundry avatar sourabhCofoundry commented on May 23, 2024

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

from react-native-onesignal.

beyzatsc avatar beyzatsc commented on May 23, 2024

This worked for me in functional component:

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

thank you.. you are my hero

from react-native-onesignal.

AliAbdullah1122 avatar AliAbdullah1122 commented on May 23, 2024

OneSignal

can you send me the code you use here

from react-native-onesignal.

AliAbdullah1122 avatar AliAbdullah1122 commented on May 23, 2024

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

from react-native-onesignal.

yepes avatar yepes commented on May 23, 2024

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

Throws an "undefined is not a function"

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

from react-native-onesignal.

nan-li avatar nan-li commented on May 23, 2024

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.

from react-native-onesignal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.