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);
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);
Share
Improve this question
edited Sep 28, 2017 at 12:07
boehm_s
5,5444 gold badges35 silver badges45 bronze badges
asked Sep 15, 2017 at 14:10
Julio SalazarJulio Salazar
1211 gold badge1 silver badge5 bronze badges
5 Answers
Reset to default 5Here'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;
});
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.
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-
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
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());