Skip to content

Instantly share code, notes, and snippets.

@dev-jaydeep
Created October 11, 2022 11:37
Show Gist options
  • Save dev-jaydeep/75f7fb38016ec4364ae7bd6565517c1f to your computer and use it in GitHub Desktop.
Save dev-jaydeep/75f7fb38016ec4364ae7bd6565517c1f to your computer and use it in GitHub Desktop.
Send FCM Push notification using PHP
<?php
/**
* Send FCM Push notification using PHP
*/
$serverKey = '';
$deviceToken = '';
$notificationBody = array(
'body' => 'here is a message. message',
'title' => 'This is title #2',
'sound' => "default",
'color' => "#203E78"
);
$postData = array(
'to' => $deviceToken,
'priority' => 'high',
'notification' => $notificationBody
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: key=' . $serverKey,
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
$result = curl_exec($ch);
curl_close($ch);
//RESULT
/*
{
"multicast_id":5557136687835857624,
"success":1,
"failure":0,
"canonical_ids":0,
"results":[
{
"message_id":"0:1665487821688077%ccaea429ccaea429"
}
]
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment