Skip to content

Instantly share code, notes, and snippets.

@mludi
Created October 31, 2018 18:01
Show Gist options
  • Save mludi/68a8a56b2e443291438da09388f6d6bd to your computer and use it in GitHub Desktop.
Save mludi/68a8a56b2e443291438da09388f6d6bd to your computer and use it in GitHub Desktop.
<?php
public function sendMessage($data, $token, $notificationType)
{
$url = 'https://fcm.googleapis.com/fcm/send';
/**
* * If you'd like to send to mulitple devices 'registration_ids' => [$tokens],
*/
$postFields = json_encode([
'to' => $token,
'data' => ['message' => $message]
]);
$headers = [
'Authorization: key=' . $this->gcmKey,
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $postFields,
CURLOPT_TIMEOUT => 30,
]);
$result = curl_exec($ch);
if ($result === false) {
throw new \Exception('Curl failed: ' . curl_error($http2ch));
}
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$jsonResult = json_decode($result);
$reason = null;
if (isset($jsonResult->results[0]->error)) {
$reason = $jsonResult->results[0]->error;
} else {
$reason = 'All Good';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment