OneSignal Get Player Id

Julio Salazar picture Julio Salazar · Sep 15, 2017 · Viewed 12.5k times · Source

I'm doing an app for a homework in my school and I'm using Onesignal REST API, but I want to save the player id in my database to use it in another application like a server sender.
My application is in intel xdk and I'm using Cordova to build on Android.
The problem is that I can't find any example getting the player id.
Can anybody help me with this problem ?

I'm using JavaScript Thanks.

this is what I have in my .js :

document.addEventListener('deviceready', function () {

  var notificationOpenedCallback = function(jsonData) {
    console.log('notificationOpenedCallback: ' + JSON.stringify(jsonData));
  };

  window.plugins.OneSignal
    .startInit("XXXXXX-XXXX-XXX-XXXX-XXXXXXXXX") // <- api id
    .handleNotificationOpened(notificationOpenedCallback)
    .endInit();

  OneSignal.push(function() {
    OneSignal.getUserId(function(userId) {
      console.log("OneSignal User ID:", userId);
    });

    OneSignal.getUserId().then(function(userId) {
      console.log("OneSignal User ID:", userId);
    });
  });
}, false);

Answer

Julio Salazar picture Julio Salazar · Oct 2, 2017

Here's a working code snippet:

window.plugins.OneSignal
    .startInit("YOUR-APP-ID")
    .handleNotificationOpened(notificationOpenedCallback)
    .endInit();

window.plugins.OneSignal.getPermissionSubscriptionState(function(status) {
    idapp = status.subscriptionStatus.userId;
});