Skip to content

Instantly share code, notes, and snippets.

@httol
Forked from IsmailShurrab/firebase notification curl
Last active January 8, 2021 08:19
Show Gist options
  • Save httol/811686c7ef3ca22507f4072dd44c50e4 to your computer and use it in GitHub Desktop.
Save httol/811686c7ef3ca22507f4072dd44c50e4 to your computer and use it in GitHub Desktop.
firebase notification curl php
<?php
function send($token,$title,$body){
$url = "https://fcm.googleapis.com/fcm/send";
$serverKey = 'your server token of FCM project';
$notification = array('title' =>$title , 'text' => $body, 'sound' => 'default', 'badge' => '1');
$arrayToSend = array('to' => $token, 'notification' => $notification,'priority'=>'high');
$json = json_encode($arrayToSend);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ch);
//Close request
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
}
// for testing
send("***","title","body");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment