• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 82
  • Last Modified:

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
1 Solution
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 = ‘https://android.googleapis.com/gcm/send&#8217;;
$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.
BRMarketingAuthor Commented:
Perfect answer Scott Fell. Thank you
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now