在页面加载时从 OneSignal 获取 player_id [英] Get player_id from OneSignal on page load

查看:79
本文介绍了在页面加载时从 OneSignal 获取 player_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Onesigal API 实施网络推送通知.

I am implementing web push notifications using Onesigal API.

    $fields = array(
        'app_id' => "07aae1f0-xxxx-xxxx-aa7a-21c79c5b6733",
        'include_player_ids'=>['57dd3f80-xxxx-xxxx-adb0-294bfa69621a'],
        'contents' => array( "en"=> "message" )
    );

    $fields = json_encode($fields);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
                                               'Authorization: Basic YTdmYTYxYzYtN...tNjVlNzJkNDFlZmMz'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem' );
    $response = curl_exec($ch);
    curl_close($ch);

如果您知道他的 player_id,上面的代码会向用户发送推送通知.但是假设一些用户访问我的网站并订阅推送通知并离开.稍后他再次访问(他已经订阅)并收藏.(添加到收藏夹)当前缺货的产品.那么我怎么能得到<我可以将这个用户的代码>player_id 存储在 "subs_fav" 表中吗?

Above code sends a push notification to the user if you know his player_id. But suppose if some user visits my site and subscribes to push notifications and leaves.Later again he visits (he is already subscribed) and favs.(adds to favorite) a currently out of stock product.So how can I get player_id of this user which I can store in "subs_fav" table ?

id  player_id           product_id
1   57dd3f80...a69621a  p097

当该产品可用时 pushFav() 函数将运行,它会通知用户该产品现在可用并且他们可以购买它,但为此我们应该知道最喜欢的用户的 player_id.这个产品.我想知道如何找到这个 player_id 以便我可以发送它 addToFav() ajax 调用以存储在 "sub_fav" 表中

When that product becomes available pushFav() function will run which would notify the user that the product is now available and they can buy it but for that we should know the player_id of the user who favd. this product. I want to know how to find this player_id so that I can send it addToFav() ajax call to store in "sub_fav" table

推荐答案

找到了!您只需要 OneSignal.getUserId() 传入一个回调函数,它会在查询时提供用户的 player_id.在致电之前,请确保用户已实际订阅.

Found it! all you need is OneSignal.getUserId() passed in a callback, which gives the user's player_id upon querying. Before calling make sure user has actually subscribed.

OneSignal.isPushNotificationsEnabled(function(isEnabled) {
  if (isEnabled) {
      // user has subscribed
      OneSignal.getUserId( function(userId) {
          console.log('player_id of the subscribed user is : ' + userId);
          // Make a POST call to your server with the user ID        
      });
  }
});

这篇关于在页面加载时从 OneSignal 获取 player_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆