How can send push notifications via PHP ?

I use shared linux hosting . I use PHP and mySql

I want to send push notifications through my shared server to the browser.
How can I do that?

How can send push notifications? thank you
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ayoub RouziCeo & CoFounderCommented:
Hello, you can use that code:
Using to send push notification message on user’s device .

function sendNotificationMessageByAdminPanel($usersID,$message){
$usersID = $usersID;
$message = $message;

// Using to send push message to the users iphone device.
function sendIphonePushMessageByAdminPanel($usersID,$message){

$usersID = $usersID;
$message = $message;

$streamContext = stream_context_create();
stream_context_set_option($streamContext, ‘ssl’, ‘local_cert’, ‘ck.pem’);
$deviceTokens = array();

$result =query(“SELECT DeviceToken FROM tblDeviceInfo WHERE Platform =’iphone’ and userID IN ($usersID) “);

$deviceTokens[] = $result[‘result’][$i][‘DeviceToken’];

foreach($deviceTokens as $key => $token){
$apns = stream_socket_client(

$load = array(
‘aps’ => array(
‘alert’ => $message,
‘badge’ => 0,
‘sound’ => ‘default’,
‘pushType’ => ‘adminMessage’

$payload = json_encode($load);
$apnsMessage = chr(0) . chr(0) . chr(32);
$apnsMessage .= pack(‘H*’, str_replace(‘ ‘, ”, $token));
$apnsMessage .= chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);

echo $payload = json_encode($load);

// Using to send push message to all Android devices.
function sendAndroidPushMessageByAdminPanel($usersID,$message){

$usersID = $usersID;
$pushMessage = $message;

$deviceTokens = array();
$result =query(“SELECT DeviceToken FROM tblDeviceInfo WHERE Platform=’android’ and userID IN ($usersID) “);

$deviceTokens[] = $result[‘result’][$i][‘DeviceToken’];

foreach($deviceTokens as $key => $registatoin_id){
$gcmRegIds = array($registatoin_id);
$message = array(“m” => $pushMessage);
sendPushNotificationToGCMByAdmin($gcmRegIds, $message);


//generic php function to send GCM push notification
function sendPushNotificationToGCMByAdmin($gcmRegIds, $message) {
//Google cloud messaging GCM-API url
$url = ‘;;
$fields = array(
‘registration_ids’ => $gcmRegIds,
‘data’ => array(“message” =>$message,’pushType’ => ‘adminMessage’ ),
‘delay_while_idle’ => true,

// Google Cloud Messaging GCM API Key
define(“GOOGLE_API_KEY”, “AIzaSyB5GYGCfpDTqO6JGpnOW5hlC9kmsrrcsiw”);
$headers = array(
‘Authorization: key=’ . GOOGLE_API_KEY,
‘Content-Type: application/json’
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die(‘Curl failed: ‘ . curl_error($ch));
//echo $result;

Open in new window

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
For sending notifications to the browser, this is done on the client side (javascript).  What you should do is start by taking some time to read up for both Chrome and Firefox

The way you get data from your database to your javascript code will look like below.

Assume you have javascript that alerts Hello World
var message = "Hello World";

Open in new window

But we want to deliver the message from our php script so it can come from a database or other source.
$message = "Hello World";
var message = "<?php echo $message ?>";

Open in new window

This works because php runs on the server before the data is sent to the browser. The php is processed and in this case all that is sent to the browser from directly above is exactly the same as the first example.

The rest is just going to read up on sending push notifications to the browser which will also include getting permissions. What I suggest is to go through the tutorial and post any questions you have when you get stuck in a new question thread.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BRMarketingAuthor Commented:
Perfect answer Scott Fell. Thank you
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.