5

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);
boehm_s
  • 5,254
  • 4
  • 30
  • 44
Julio Salazar
  • 111
  • 1
  • 1
  • 5

5 Answers5

5

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;
});
Edric
  • 24,639
  • 13
  • 81
  • 91
Julio Salazar
  • 111
  • 1
  • 1
  • 5
5

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.

David Toledo
  • 484
  • 6
  • 13
  • 1
    this is the correct way to get the player id/token of user – MSD Feb 04 '20 at 11:13
  • 1
    it works thanks bro...for more function you can console window.plugin.OneSignal we can use any functions of onesignal Cordova sdk. – Munaf Hajir Mar 31 '20 at 12:52
3

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-

0

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

Ricky Riccs
  • 41
  • 1
  • 5
0

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());
Xfox
  • 174
  • 2
  • 10