Julio Salazar
Julio Salazar

Reputation: 121

OneSignal Get Player Id

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

Upvotes: 6

Views: 25056

Answers (5)

Xfox
Xfox

Reputation: 361

In order to debug I use this snippet:

console.log("Site notification permission: ", await OneSignal.getNotificationPermission());
console.log("Push enabled: ", await OneSignal.isPushNotificationsEnabled());
console.log("Player id: ", await OneSignal.getUserId());

Upvotes: 0

Ricky Riccs
Ricky Riccs

Reputation: 31

after installing oneSignal plugin, you can get a player id using this function in the console.

await OneSignal.getUserId();

https://documentation.onesignal.com/docs/users-and-devices#finding-users

Upvotes: 0

David Toledo
David Toledo

Reputation: 494

Add this block of code after the endInit() method:

        window.plugins.OneSignal.getIds(function(ids) {
            // Player ID will be available at the object ids.userId
        });

Here's a complete example on how you can display the player ID in an alert!

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

            // Enable to debug issues.
            // window.plugins.OneSignal.setLogLevel({logLevel: 4, visualLevel: 4});

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

            window.plugins.OneSignal
                .startInit("YOUR_APP_ID_HERE")
                .handleNotificationOpened(notificationOpenedCallback)
                .endInit();

            window.plugins.OneSignal.getIds(function(ids) {
                alert("player id: " + ids.userId);
            });

        }, false);

Don't forget to replace YOUR_APP_ID_HERE by your real app id.

Upvotes: 5

Julio Salazar
Julio Salazar

Reputation: 121

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

Upvotes: 5

Varun Bargali
Varun Bargali

Reputation: 39

OneSignal prototype provides a function getIds which gives the player id and push token for the current device.

window.plugins.OneSignal
    .startInit("XXXXXX-XXXX-XXX-XXXX-XXXXXXXXX") <- api id
    .getIds(function(userDetails) {
        console.log(userDetails.userId); // Player ID
        console.log(userDetails.pushToken);
    })
    .endInit();

https://documentation.onesignal.com/docs/cordova-sdk#section--postnotification-

Upvotes: 3

Related Questions