I noticed that OneSignal is creating a unique player_id and adding a column in its database when my web app is loaded for the first time by any user.
I’m trying to add a custom external_user_id into the same one signal DB field using below code (as per OneSignal docs):
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" ></script>
<script>
let externalUserId = "123456789";
OneSignal.push(function() {
OneSignal.setExternalUserId(externalUserId);
});
</script>
But somehow, the external_user_id field is still set to empty.
So I added the below code to it.
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" ></script>
<script>
let externalUserId = "123456789";
window.OneSignal = window.OneSignal || [];
OneSignal.push(function() {
OneSignal.init({
appId: "xoxoxoxoxoxoxoxoxo",
});
});
OneSignal.push(function() {
console.log("triggering");
OneSignal.setExternalUserId(externalUserId);
console.log("triggering");
});
It has no errors, yet external_user_id is still not updated.
This is the OneSignal code I have added to my Android Studio
package com.appgrep.xoxoxo;
import android.app.Application;
public class OneSignal extends Application {
private static final String ONESIGNAL_APP_ID = "xoxoxoxoxoxoxoxoxo";
@Override
public void onCreate() {
super.onCreate();
// OneSignal Initialization
com.onesignal.OneSignal.initWithContext(this);
com.onesignal.OneSignal.setAppId(ONESIGNAL_APP_ID);
}
}
I just am trying to add External_User_Id in OneSignal using Web (JS)
Any help is greatly appreciated.
Amit’s suggestion to get a user id
var OneSignal = window.OneSignal || [];
OneSignal.push(function() {
OneSignal.getUserId(function(userId) {
console.log("OneSignal User ID:", userId);
$("#some").text(userId);
});
});
the getUserId
function is not triggereing.
I can do like:
var OneSignal = window.OneSignal || [];
OneSignal.push(function() {
OneSignal.on('subscriptionChange', function (isSubscribed) {
OneSignal.push(function() {
OneSignal.getUserId(function(userId) {
console.log("OneSignal User ID:", userId);
});
});
});
});
But in my scenario, there is no subscription, all users are registered in one signal as soon as their open the web app and the subscription is auto-ticked.
2
Answers
I think it’s not possible through web buddy.
What you are trying to do is to update a field in onesignl using website. Which i don’t think one signal let’s you do.
I have tried setting external_user_id using web(js) and it’s working fine without problem.
Requirement
Now just open the Dev console and paste the following code to set external_user_id
Now check if external_user_id is set or not
See screenshot of dev console
alternatively, visit OneSignal dashboard and check the user, you will external_user_id just make sure to make external_user_id column visible,
Check external_user_id in dashboard image
When done with your testing remove the demo external_user_id
Documentation: https://documentation.onesignal.com/docs/external-user-ids
Just keep in mind if you are setting External_user_id through Android SDK and want to update it on web
Use the following code
Replace <APP_ID> with your app app id of the page