Skip to content

Instantly share code, notes, and snippets.

@ProdigyTech
Last active April 2, 2017 17:16
Show Gist options
  • Save ProdigyTech/5fe9a64304ce31b49a3081ece19cfba3 to your computer and use it in GitHub Desktop.
Save ProdigyTech/5fe9a64304ce31b49a3081ece19cfba3 to your computer and use it in GitHub Desktop.
Simple PHP script to send an Android Device push notifications using Firebase Cloud Messaging API
class PushNotifications{
private static $API_ACCESS_KEY = "YOUR_FCM_SERVER_KEY ";
public function __construct() {
// exit('Init function is not allowed');
}
public function android($data, $reg_id) {
$url = 'https://fcm.googleapis.com/fcm/send';
$message = array
(
'title' => $data['mtitle'],
'body' => $data['mdesc'],
'subtitle' => '',
'tickerText' => '',
'msgcnt' => 1,
'vibrate' => 1
);
$headers = array(
'Content-Type:application/json',
'Authorization:key='.self::$API_ACCESS_KEY
);
$fields = array(
'notification' => $message,
'to' => $reg_id,
);
return self::useCurl($url, $headers, $fields);
}
private function useCurl($url, $headers , $fields) {
$ch = curl_init();
if ($url) {
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ($fields) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
}
// Execute post
$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
//checking the response code we get from fcm for debugging purposes
echo "http response " . $httpcode;
//checking the status/result of the push notif for debugging purposes
echo $result;
return $result;
}
}
}
function SendToDevice($title, $message, $regId)
{
require_once('TestNotifications.php');
// Message payload, using data from post request
$msg_payload = array(
'mtitle' => $title,
'mdesc' => $message,
);
$var = new PushNotifications();
$var->android($msg_payload, $regId);
}
if ( $_SERVER['REQUEST_METHOD'] == 'POST' && (!empty($_POST['SendTo']))) {
SendToDevice($_POST['title'], $_POST['messageBody'], $_POST['SendTo']);
}
<!-- HTML Form that contains input fields to send notification data in POST request on submit -->
<!DOCTYPE HTML>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<h1>Test FCM from server to client! </h1>
</header>
<form method="POST" action = "TestNotifications.php">
Who do you want to send the alert to?
<select id = "SendTo" name="SendTo">
<!-- Put the FCM registration token in option value -->
<option value="FCM registration token">Device 1</option>
</select>
<br />
Title: <input tye="text" name ="title" id="title">
<br />
Message Body : <input type = "text", name = "messageBody">
<br />
<input type="submit" name="submit" id="submit">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment