Skip to content

Instantly share code, notes, and snippets.

@aemxn
Last active February 28, 2017 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aemxn/ce613b51f82a682d63266bb699397fda to your computer and use it in GitHub Desktop.
Save aemxn/ce613b51f82a682d63266bb699397fda to your computer and use it in GitHub Desktop.
Simple FCM push notification PHP snippet
<?php
$to =
"USER_PUSH_TOKEN_HERE";
$fields = array(
'to' => $to,
// custom payload for a client to handle
// use `notification` array for more general predefined key/value payload instead
'data' => array(
"title" => "test notification title " . rand(1, 1000),
"body" => "Christopher Edward Hansen (born September 13, 1959)[2] is an American television journalist. He is known for his work on Dateline NBC, in particular the former segment To Catch a Predator, which revolved around catching potential Internet sex predators using a sting operation. His also hosts Killer Instinct on Investigation Discovery, which documents homicide investigations.[3] He is also the new host of the syndicated show Crime Watch Daily. [4]<br/><br/><img src=\"http://www.kualalumpurpost.net/wp-content/uploads/2013/10/tpeli.jpg\">",
"type" => "news",
"timestamp" => date('d F Y h:i a'),
"image_url" => "https://forum.lowyat.net/uploads/attach-9/post-2409-1400139876_thumb.jpg",
"transaction_guid" => rand(999, 9999),
"guid" => rand(10000, 99999),
"total_unread" => "32"
)
);
$headers = array(
'Authorization: key=KEY_HERE',
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, "https://fcm.googleapis.com/fcm/send");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result . "<hr/>";
var_dump($fields);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment