Skip to content

Instantly share code, notes, and snippets.

@jtwiest
Created July 14, 2016 00:54
Show Gist options
  • Save jtwiest/74caa5f7fc09b7ae29a96077eb42f607 to your computer and use it in GitHub Desktop.
Save jtwiest/74caa5f7fc09b7ae29a96077eb42f607 to your computer and use it in GitHub Desktop.
Firebase Cloud Messaging Curl Wrapper
<?php
namespace Firebase;
class CloudMessaging
{
public static $authKey = '';
public static function sendMessage($title, $body, $to, $data = [], $priority = 'high')
{
$fields = [
'notification' => [
'title' => $title,
'body' => $body,
'sound' => 'default',
'click_action' => 'FCM_PLUGIN_ACTIVITY',
'icon' => 'fcm_push_icon'
],
'to' => $to,
'priority' => $priority
];
if ($data) {
$fields['data'] = $data;
}
$fields = json_encode($fields);
$ch = curl_init('https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization:key=' . static::$authKey
));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment