Created
December 10, 2017 12:35
-
-
Save IsmailShurrab/9927684646f07d83c14af4b92be9e6fb to your computer and use it in GitHub Desktop.
firebase notification curl php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
?> |
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
$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,
$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
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
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
how can i get response object and save into databese.....?