【问题标题】:OneSignal - How to get PlayerId after user subscribes?OneSignal - 用户订阅后如何获取 PlayerId?
【发布时间】:2017-01-25 11:16:23
【问题描述】:

在我的javascript代码中,我要求用户使用OnceSignal服务订阅网站通知,所以我这样做了:

    OneSignal.registerForPushNotifications();

然后我尝试使用以下内容捕获订阅用户的 PlayerId:

    OneSignal.push(function() {
        OneSignal.on('notificationPermissionChange', function(permissionChange) {
            var currentPermission = permissionChange.to;

            if (currentPermission == "granted") {
                OneSignal.getUserId(function(userId) { 
                    console.log(userId);
                });
            }
        });

然后我获取该用户 ID 并将其存储在我的 mysql 数据库中,以便稍后发送通知。但是由于某种原因,userId 与存储在 OneSignal Server 上的 PlayerId 不同,而格式相同。 UserId 和 PlayerId 是不同的东西吗?如果是这样,用户订阅Pushes后如何获取PlayerId?

【问题讨论】:

    标签: push-notification onesignal


    【解决方案1】:

    您很可能看到的是旧用户 ID。 notificationPermissionChange 事件发生得太早,无法获取用户的 ID。该事件仅用于分析,如果您想在订阅后查找用户 ID,请使用 subscriptionChange 事件。

    SDK 文档链接:https://documentation.onesignal.com/docs/web-push-sdk

    【讨论】:

    • 谢谢。它们似乎仍然不同。如果我将 OneSignal.getUserId(function(userId) { ... 放在 OneSignal.push(["getNotificationPermission", function(permission) { 或 OneSignal.push(function() { OneSignal.on('subscriptionChange', function ( isSubscribed) {...我在控制台中得到相同的值,例如:3c4d2ae9-de39-4780-92fb-76667f7681ec 但在服务器上它仍然不同:f2736015-ffca-4cef-bf03-b5708bdb1504
    • 好的,我现在明白发生了什么。它从缓存中给了我一个旧的 UserId。如果我完全清除缓存,我会得到与服务器匹配的正确 UserId。你是对的,没有 notificationPermissionChange 发生得太早,当我清除浏览器缓存时,我在该事件中得到 UserId = null 并且我在 subscriptionChange 事件中得到了我需要的东西,非常感谢
    • 缓存?现在我不能告诉用户清除缓存以使用该应用程序。这里有什么更好的解决方案?
    【解决方案2】:

    以下内容过去运行良好,但最近在绑定到“subscriptionChange”事件时根本无法获取玩家 ID。

    事实上,在事件监听器中,“OneSignal.push”执行后什么都没有。

    尝试删除它并直接进入“OneSignal.getUserId”,但似乎承诺没有返回任何内容。

    OneSignal.on('subscriptionChange', function (isSubscribed) {
                console.log('is subscribed', isSubscribed);
                OneSignal.push(function() { 
                  console.log('attempt to get id'); // doesn't get this far
                  OneSignal.getUserId(function(userId) {
                    console.log('user id', userId); // doesn't get this far                
                  });
                });
              });

    奇怪的是,如果将其粘贴到控制台中,可以很好地获取玩家 ID,只有在回调期间没有返回任何内容。

     OneSignal.getUserId(function(userId) {
                    console.log('user id', userId);              
                  });

    看起来真的很奇怪为什么代码刚刚停止工作,因为我们使用这个确切的代码一年多了。

    有什么建议吗?

    【讨论】:

    • OK 让它工作,不确定确切的问题,但是,我怀疑这有一段时间没有工作,但没有引起注意。最终使用新列表创建了一个新站点并启用了“我的站点不是完全 HTTPS”。然后上传服务工作人员文件,配置服务工作人员(托管在子目录中并为 / 设置服务工作人员标头) - 显然我们不在根目录中托管 3rd 方库!现在她又开始工作了。我的猜测是它在工作时是通过 http 进行的。
    【解决方案3】:

    你好,这是我用 js 完成的 问候,我希望它对你有用。

    window.plugins.OneSignal
                .addSubscriptionObserver(function(state){
                var datos = JSON.stringify(state);
                var valor = JSON.parse(datos); 
                var playerId = valor.to.userId
                console.log( playerId + ' *_IF_**PLAYER ID***' )                
    });
    

    【讨论】:

      猜你喜欢
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-23
      • 2017-04-07
      • 2021-12-24
      • 1970-01-01
      相关资源
      最近更新 更多