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); | |
?> |
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
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