OneSignal Push Notification PHP API – Technology

OneSignal Push Notification PHP API – Technology

In this post we will give you information about OneSignal Push Notification PHP API – Technology. Hear we will give you detail about OneSignal Push Notification PHP API – TechnologyAnd how to use it also give you demo for it if it is necessary.

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.

Another must read:  How to Open URL in New Tab using c#
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

Step 1: Include header Part

index.html

<!DOCTYPE html>
<html>
<head>
    <title>onesignal - Register Notification - onlinecode</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

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

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://onlinecode.org/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 - onlinecode free Download Source code'
    );
    $beutiful_template_array = array();
    array_push($beutiful_template_array, array(
        "id" => "file-button",
        "text" => "onlinecode",
        "icon" => "http://i.imgur.com/N8SN8ZS.png",
        "url" => "https://onlinecode.org/example/"
    ));
    array_push($beutiful_template_array, array(
        "id" => "file-button-2",
        "text" => "onlinecode2",
        "icon" => "http://i.imgur.com/N8SN8ZS.png",
        "url" => "https://onlinecode.org/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 :

Another must read:  Simple PHP Create Custom Captcha Generator

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

Hope this code and post will helped you for implement OneSignal Push Notification PHP API – Technology. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

39  +    =  44

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US