Skip to content

Instantly share code, notes, and snippets.

@iespino00
Created August 30, 2017 22:47
Show Gist options
  • Save iespino00/afa2c22e7c84d1b5690e6657613aa841 to your computer and use it in GitHub Desktop.
Save iespino00/afa2c22e7c84d1b5690e6657613aa841 to your computer and use it in GitHub Desktop.
Push notification desde PHP - Firebase.
<?php
function send_notification($tokens, $message)
{
$url='https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = AOyUEjHurDZNKM-D7hcGG7wnSwSEY_ngA4P-w',
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if($result === FALSE)
{
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
$conn = mysqli_connect("localhost","root","Password","fcm");
$sql = "select token from users";
$result = mysqli_query($conn,$sql);
$tokens = array();
if(mysqli_num_rows($result) > 0)
{
while ($row = mysqli_fetch_assoc($result))
{
$tokens[] = $row["Token"];
}
}
mysqli_close($conn);
$message = array("message" => "FCM PUSH NOTIFICATION TEST");
$message_status = send_notification($tokens, $message);
echo $message_status;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment