Created
July 14, 2016 00:54
-
-
Save jtwiest/74caa5f7fc09b7ae29a96077eb42f607 to your computer and use it in GitHub Desktop.
Firebase Cloud Messaging Curl Wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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