如何使用Web(JS)在OneSignal中设置external_user_id?

发布于 2025-02-11 12:02:17 字数 2707 浏览 2 评论 0原文

我注意到,当任何用户首次加载我的Web应用程序时,OneSignal正在创建唯一的 player_id ,并在其数据库中添加列。

我正在尝试使用以下代码将自定义 external_user_id 添加到同一信号db字段中(按 onsignal docs ):

<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" ></script>
<script>
let externalUserId = "123456789"; 

OneSignal.push(function() {
  OneSignal.setExternalUserId(externalUserId);
});
</script>

但是,某种程度上, external_user_id field仍设置为空。

因此,我在其中添加了以下代码。

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

它没有错误,但 external_user_id 仍未更新。

这是我添加到 android Studio 的Insignal代码

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

我只是尝试使用 web(js) 任何帮助将不胜感激。

Amit的建议获得用户ID

var OneSignal = window.OneSignal || [];
 OneSignal.push(function() {
    OneSignal.getUserId(function(userId) {
      console.log("OneSignal User ID:", userId);
      $("#some").text(userId);
    });
  });

getUserid函数不是触发。

我可以喜欢:

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

但是在我的情况下,没有订阅,所有用户在打开网络应用程序后立即注册一个信号,并且订阅是自动挑选的。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失眠症患者 2025-02-18 12:02:17

我尝试使用Web(JS)设置external_user_id,并且毫无问题地工作正常。

要求

  • 工作OneSignal实现
  • 订阅通知

现在只需打开Dev Console并粘贴以下代码设置external_user_id

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

现在检查external_user_id是否设置了

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

请参阅Dev Console的屏幕截图

另外,请访问OneSignal仪表板并检查用户,您将external_user_id只需确保使外部_USER_ID列可见,
检查仪表板图像中的external_user_id

使用测试完成时,请删除demo externe_user_id

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

文档:< “ https://documentation.onesignal.com/docs/external-user-ids” rel =“ nofollow noreferrer”> https://documentation.onesignal.com/docs/external-user-user-ids

请注意,如果您要通过Android SDK设置external_user_id,并且想在Web上更新它

警告 - Android SDK数据同步

Onesignal Android SDKS在external_user_id和
数据标签。

如果通过编辑设备API端点设置external_user_id,则
使用相同的端点在用户上删除external_user_id
登录应用程序。

除非
首先使用setExternaluserid方法设置external_user_id
android。

这仅适用于Onesignal Android移动应用SDK。

使用以下代码

替换&lt; app_id&gt;使用页面的应用程序ID

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

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>
玩心态 2025-02-18 12:02:17

我认为通过Web Buddy不可能。

您要做的就是使用网站更新Oneignl中的字段。我认为这不是一个信号,让您做。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文