Skip to content

Instantly share code, notes, and snippets.

@mgks
Created December 1, 2024 07:55
Show Gist options
  • Save mgks/bbca55588ac222459c1fa184cacfaff5 to your computer and use it in GitHub Desktop.
Save mgks/bbca55588ac222459c1fa184cacfaff5 to your computer and use it in GitHub Desktop.
PHP client script to push firebase notifications using cURL.
<?php
//client script
$FCM_KEY = "__YOUR_FIREBASE_AUTH_KEY__";
function fcm_push($token, $title, $text, $uri, $img='', $custom_data=[]){
global $FCM_KEY;
$url = 'https://fcm.googleapis.com/fcm/send';
$headers = array('Content-Type: application/json', 'Authorization: key='.$FCM_KEY);
$body = array(
'notification' => array(
'title' => $title,
'text' => $text,
'click_action' => 'Open_URI'
),
'data' => array(
'title' => $title,
'uri' => $uri,
'image' => $img
),
'to' => $token
);
$data = json_encode($body);
$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, $data);
$result = curl_exec($ch);
return $result;
curl_close($ch);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment