Skip to content

Instantly share code, notes, and snippets.

@alpenzoo
Created August 24, 2017 12:21
Show Gist options
  • Save alpenzoo/a08cf192bc622fb03afe95b30b956392 to your computer and use it in GitHub Desktop.
Save alpenzoo/a08cf192bc622fb03afe95b30b956392 to your computer and use it in GitHub Desktop.
Firebase FCM from PHP the easy way. Send payload as desired. Returns execution result. Just a gist, no production code.
<?php
$deviceToken = $_POST['dt'];
$body = $_POST['b'];
if (empty($deviceToken)){
die ("nothing to do, no device. exit");
}
#API access key from Google API's Console
define( 'API_ACCESS_KEY', '' );
#prep the bundle
$msg = array
(
'body' => $body,
'title' => 'notification',
'icon' => 'myicon',/*Default Icon*/
'sound' => 'mySound'/*Default sound*/
);
$dataMsg = array
(
'phone' => array("no" => "12", "label" => "xxx"),
'data1' => 'ggg',
'data2' => 'hhhh'
);
$jsonArray = array
(
'to' => $deviceToken,
'notification' => $msg,
'data' => $dataMsg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
echo "<hr><code><pre>";
echo json_encode( $jsonArray );
echo "</pre></code><hr>";
#Send Reponse To FireBase Server
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $jsonArray ) );
$result = curl_exec($ch );
curl_close( $ch );
#Echo Result Of FireBase Server
echo $result;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment