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
LVL 1
BRMarketingAsked:
Who is Participating?

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

x
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;
sendIphonePushMessageByAdminPanel($usersID,$message);
sendAndroidPushMessageByAdminPanel($usersID,$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) “);

for($i=0;$i<count($result[‘result’]);$i++){
$deviceTokens[] = $result[‘result’][$i][‘DeviceToken’];
}

foreach($deviceTokens as $key => $token){
$apns = stream_socket_client(
‘ssl://gateway.push.apple.com:2195’,
$error,
$errorString,
60,
STREAM_CLIENT_CONNECT, $streamContext);

$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);
}

if(!empty($payload)){
echo $payload = json_encode($load);
}
fclose($apns);
}

// 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) “);

//count($result[‘result’]);
for($i=0;$i<count($result[‘result’]);$i++){
$deviceTokens[] = $result[‘result’][$i][‘DeviceToken’];
}

foreach($deviceTokens as $key => $registatoin_id){
//$registatoin_id=’APA91bHyWDK8sceYe2R1-GnwSfvV3MHOSkguVTsBzGn4OL86AQrxH15MhS2y9fATu5YOljl8v3Tze0xIZymHtBTPqFRLXO5d9j3Zx3xoomfi7_GSBKA9J2MHUPPX8EH9AyAbtSz6fPsTtR34KacSWiZJzWDQhNUWuQ’;
$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));
}
curl_close($ch);
//echo $result;
}

Open in new window

0
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

https://developers.google.com/web/fundamentals/push-notifications/
https://developers.google.com/web/ilt/pwa/introduction-to-push-notifications-slides
https://developers.google.com/web/fundamentals/codelabs/push-notifications/

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

Assume you have javascript that alerts Hello World
<script>
var message = "Hello World";
alert(message);
</script>

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.
<?php
$message = "Hello World";
?>
<script>
var message = "<?php echo $message ?>";
alert(message);
</script>

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

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
0
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
Linux

From novice to tech pro — start learning today.