Skip to content

Instantly share code, notes, and snippets.

@IsmailShurrab
Created December 10, 2017 12:35
Show Gist options
  • Save IsmailShurrab/9927684646f07d83c14af4b92be9e6fb to your computer and use it in GitHub Desktop.
Save IsmailShurrab/9927684646f07d83c14af4b92be9e6fb to your computer and use it in GitHub Desktop.
firebase notification curl php
<?php
$url = "https://fcm.googleapis.com/fcm/send";
$token = "your device token";
$serverKey = 'your server token of FCM project';
$title = "Notification title";
$body = "Hello I am from Your php server";
$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);
?>
@Muzamil-khan
Copy link

how can i get response object and save into databese.....?

@IsmailShurrab
Copy link
Author

how can i get response object and save into databese.....?

you can access to response via this $response :
$response = curl_exec($ch);

then you can save any data to database

@Muzamil-khan
Copy link

$response just give me true or false value..... when i decode it with json then it gives 1 or 0.. but i cannot get full json response,

@IsmailShurrab
Copy link
Author

$response just give me true or false value..... when i decode it with json then it gives 1 or 0.. but i cannot get full json response,

yes , response boolean
if true mean successfully sent
else fail

@josdiv
Copy link

josdiv commented May 16, 2021

On line 7 of the code, that array key 'text' => $body is not correct and will give an undefined message result.

It should be 'body' => $body

@jun0925
Copy link

jun0925 commented Oct 8, 2021

Hello.
To get the return value "curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);"
Just add options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment