OneSignal Push Notification PHP API

Today, We want to share with you OneSignal Push Notification PHP API.In this post we will show you Create Onesignal Push Notification in PHP, hear for Adding Push Notification in PHP With OneSignal we will give you demo and example for implement.In this post, we will learn about OneSignal Push Notification Send to Specific User in PHP with an example.

OneSignal Push Notification PHP API

There are the Following The simple About OneSignal Push Notification PHP API Full Information With Example and source code.

As I will cover this Post with live Working example to develop Push Notification PHP via Onesignal, so the OneSignal how to send Push to specific device for this example is following below.

onesignal php push notification panel

onesignal features

  • Unlimited Mobile Push
  • Web Push Up to 30K Subscribers
  • Unlimited Segmentation
  • Delivery Scheduling
  • Emojis and Images
  • Localization
  • A/B Testing
  • Real-Time Analytics
OneSignal Push Notification PHP API Examples
OneSignal Push Notification PHP API Examples

Step 1: Include header Part

index.html

<!DOCTYPE html>
<html>
<head>
    <title>onesignal - Register Notification - Pakainfo.com</title>
</head>
<body>
<link rel="manifest" href="manifest.json" />
    <script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>
    <script>
      var OneSignal = window.OneSignal || [];
      OneSignal.push(function() {
     OneSignal.init({
          appId: "5df07b13-d892-7jk6-b094-9898af0a7e95",
        });
      });
  OneSignal.push(function() {
  OneSignal.on('subscriptionChange', function(isSubscribed) {
    if (isSubscribed) {
      OneSignal.getUserId(function(userId) {
      });
    }else{
        console.log("Thanks a lot!! unsubscribed");
    }
  });
});
</script>
</body>
</html>

Step 2: Include header Part

Ajax Call Save playerId

    <script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async=""></script>
    <script>
      var OneSignal = window.OneSignal || [];
      OneSignal.push(function() {
        OneSignal.init({
          appId: "5df07b13-d892-7jk6-b094-9898af0a7e95",
          autoRegister: true,
          safari_web_id:'web.onesignal.auto.9898c99c-fc92-40e3-8bkk-b2b98256062d',
        });
      });
      
  OneSignal.push(function() {
  OneSignal.on('subscriptionChange', function(isSubscribed) {
    if (isSubscribed) {
      OneSignal.getUserId(function(userId) {
        // Make a PHP POST call to your PHP Web server with the user ID
        $.ajax({
            type:'get',
            url : 'https://www.pakainfo.com/add_user_all_subs.php',
            data : {'playerId':userId},
            success:function(response){
              console.log(response);
            }
        })
      });
    }else{
        console.log("sorry dear, unsubscribed");
    }
  });
});
</script>

Step 3: onesignal Add New Subscribers

add_user_all_subs.php

<?php 
//Database connection
require 'includes/config.php';

    if(isset($_REQUEST['playerId']) and $_REQUEST['playerId'] != ""){
        // echo $sql_qry = "INSERT INTO push_onesignal_message SET cat_id = " . $_SESSION[CAT_TYPE.'cat_id'] . " AND player_id = '" . $_REQUEST['playerId'] ."'";
        $id = $_SESSION[CAT_TYPE.'cat_id'];
        echo $sql_qry = "INSERT INTO push_onesignal_message(cat_id,player_id) VALUES($id,'".$_REQUEST['playerId']."')";
        mysqli_query($link,$sql_qry);
    }else{
        return "Sorry, You did not get player id";
    }
?>

Step 4: onesignal Send Push Notification Code

sendpushnotification.php

<?php
function sendPushNotification() {
    $content      = array(
        "en" => 'Welcome To - pakainfo.com free Download Source code'
    );
    $beutiful_template_array = array();
    array_push($beutiful_template_array, array(
        "id" => "file-button",
        "text" => "pakainfo",
        "icon" => "http://i.imgur.com/N8SN8ZS.png",
        "url" => "https://www.pakainfo.com/example/"
    ));
    array_push($beutiful_template_array, array(
        "id" => "file-button-2",
        "text" => "Pakainfo2",
        "icon" => "http://i.imgur.com/N8SN8ZS.png",
        "url" => "https://www.pakainfo.com/example/"
    ));
    $fields = array(
        'app_id' => "cd9e4242-cd78-9898-b181-0ef50f0jd43d",
        'included_segments' => array(
            'All'
        ),
        'data' => array(
            "foo" => "bar"
        ),
        'contents' => $content,
        'web_buttons' => $beutiful_template_array
    );
    
    $fields = json_encode($fields);
    print("\nJSON sent:\n");
    print($fields);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/push_onesignal_message");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json; charset=utf-8',
        'Authorization: Basic JDKiNzU9PLKtN2M5OS00FJK7LTg6YWYtYTY1OTAzMJS8YzI8'
    ));
    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_SSL_VERIFYPEER, FALSE);
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    return $response;
}

$results = sendPushNotification();

var_dump($results);

?>

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about OneSignal Push Notification PHP API.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Top 10 Tips Advanced PHP Developers
  2.   Laravel Selecting and Aliasing Columns in Eloquent's query
  3.   How to count files in a directory using PHP Laravel?
  4.   codeigniter count rows in view & controller
  5.   Laravel Group By Clause/Statement
  6.   Pattern programs in GO Example
  7.   FontSpace Web Application Icons
  8.   PHP Laravel MySQL CRUD Application
  9.   Convert Array to XML and XML to Array using PHP
  10.   Create User Login System using C# Examples