Hi friend, today I am going to discuss a topic about push notification in php.So before a start, we need to know what is push notification?
Definition of push notification
Push notification is the delivery of information from a software application or web server to a computing device without a specific request from the client.Each an every website or application using push notification for showing new product or new article has been posted to their site or their app.
Client side script
Client side script onesignal
So first of all register to onesignal.Then create a new app in onesignal to get api key from here.We are going to discuss about javascript library here.
Download the manifest.json from onesignal add inside the <head> tag.If you use manifest.json inside body an error will be occurred. Here is the reference.
<link rel="manifest" href="/manifest.json">
Then add the SDK CDN of onesignal.
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async></script>
<script> var OneSignal = window.OneSignal || []; //Here we are initializing OneSignal.push(["init", { appId: "YOUR APP ID", subdomainName: 'datainflow', autoRegister: true, promptOptions: { actionMessage: "We'd like to show you notifications for the latest article.", acceptButtonText: "ALLOW", cancelButtonText: "NO" } }]); </script> <script> function subscribe() { OneSignal.push(["registerForPushNotifications"]); event.preventDefault(); } function unsubscribe(){ OneSignal.setSubscription(true); } var OneSignal = OneSignal || []; OneSignal.push(function() { OneSignal.on('subscriptionChange', function (isSubscribed) { console.log("The user's subscription state is now:", isSubscribed); OneSignal.sendTag("user_id","<?php echo $_SESSION['user_id']?>", function(tagsSent) { console.log("Tags have finished sending!"); }); }); var isPushSupported = OneSignal.isPushNotificationsSupported(); if (isPushSupported) { OneSignal.isPushNotificationsEnabled().then(function(isEnabled) { if (isEnabled) { console.log("Push notifications are enabled!"); } else { OneSignal.showHttpPrompt(); //Here this section is for showing notification prompt. console.log("Push notifications are not enabled yet."); } }); } else { console.log("Push notifications are not supported."); } }); </script>
This is the javascript code for showing push notification and subscribe a user for push notification.Now we are going to the sever side script, from where we are generating the notification.
Server side script
Server side script onesignal
Here we are using curl and api of onesignal.
<?PHP function sendMessage(){ $content = array( "en" => 'Your content' ); $fields = array( 'app_id' => "YOUR APP ID", 'filters' => array(array("field" => "tag", "key" => "user_id", "relation" => "=", "value" => "10"),array("operator" => "OR"),array("field" => "tag", "key" => "user_id", "relation" => "=", "value" => "11")), 'data' => array("foo" => "bar"), 'contents' => $content ); $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 NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj')); 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; } $response = sendMessage(); $return["allresponses"] = $response; $return = json_encode( $return); print("\n\nJSON received:\n"); print($return); print("\n"); ?>
That’s all.
I am trying to follow most of your recent php techniques. It would be my plearsure if you add “demo video” or something to demonstrate how your code works.. Bounce rate will also decreases i think. Thanks
we will do it soon.Thanks for the reply.