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
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.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.