Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get player_id from OneSignal on page load

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

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

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

like image 786
Vinay Avatar asked Mar 21 '17 06:03

Vinay


People also ask

How do I find my player ID from OneSignal?

Find your device for Web Mobile Add this code: await OneSignal. getUserId(); You will see your OneSignal player ID logged to the console if you have a device record.

Does OneSignal sell data?

OneSignal does not share SDK information with third parties except those who process the data on behalf of OneSignal. Data is only shared with third parties if: If an End User or Client requests or authorizes.

How do I test my OneSignal notification?

Install Your SDK Copy the alpha numeric code that OneSignal generates called Your App ID in the image below. On Thunkable, open the Push Notifications dialog and paste this ID into the Android App ID field. Click the Live Test button.

How do I find my OneSignal REST API key?

Visit Settings > Keys & IDs and click Generate New API Key to destroy the current REST API key and generate a new one.


1 Answers

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        
      });
  }
});
like image 151
Vinay Avatar answered Oct 12 '22 20:10

Vinay