skip to Main Content

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.

OneSignal user's screenshot

2

Answers


  1. 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.

    Login or Signup to reply.
  2. I have tried setting external_user_id using web(js) and it’s working fine without problem.

    Requirement

    • Working Onesignal implementation
    • Subscribe to Notification

    Now just open the Dev console and paste the following code to set external_user_id

    let externalUserId = "123456789"; // You will supply the external user id to the OneSignal SDK
    OneSignal.push(function() {
          OneSignal.setExternalUserId(externalUserId);
        });
    

    Now check if external_user_id is set or not

    OneSignal.push(function() {
      OneSignal.getExternalUserId().then(function(externalUserId){
        console.log("externalUserId: ", externalUserId);
      });
    });
    
    

    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

    OneSignal.push(function() {
      OneSignal.removeExternalUserId();
    });
    

    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

    Warning – Android SDK Data Synchronization

    The OneSignal Android SDKs leverage cacheing on external_user_id and
    Data Tags.

    If setting external_user_id through the Edit device API Endpoint, then
    use the same endpoint to remove the external_user_id upon the user
    logging out of the app.

    The removeExternalUserId method will not work unless the
    external_user_id is set first with the setExternalUserId method on
    Android.

    This is only applicable on the OneSignal Android Mobile App SDKs.

    Use the following code

    Replace <APP_ID> with your app app id of the page

    <script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" ></script>
    
    <script>
      window.OneSignal = window.OneSignal || [];
      OneSignal.push(function() {
        OneSignal.init({
          appId: "<APP_ID>",
        });
      });
    
    OneSignal.getUserId((userId) => {
        if (userId == null){
            alert("Subscribe to Notification First")
            return
        }
        alert(`Player id: is ${userId}`);
    });
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search