• 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
0
BR
Asked:
BR
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;
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
 
BRMarketingAuthor Commented:
Perfect answer Scott Fell. Thank you
0
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