Created
July 4, 2017 08:16
-
-
Save anta40/659eedbdd672c4ef745ce83d565fe4ea to your computer and use it in GitHub Desktop.
Firebase
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 | |
class Firebase { | |
// sending push message to single user by firebase reg id | |
public function send($to, $message) { | |
$fields = array( | |
'to' => $to, | |
//'data' => $message, | |
'data' => array("message" => $message)); | |
return $this->sendPushNotification($fields); | |
} | |
// Sending message to a topic by topic name | |
public function sendToTopic($to, $message) { | |
$fields = array( | |
'to' => '/topics/' . $to, | |
//'data' => $message, | |
'data' => array("message" => $message) | |
); | |
return $this->sendPushNotification($fields); | |
} | |
public function sendNews($to, $title, $body){ | |
$fields = array( | |
'to' => $to, | |
'notification' => array("title" => $title, | |
"body" => $body)); | |
return $this->sendPushNotification($fields); | |
} | |
// sending push message to multiple users by firebase registration ids | |
public function sendMultiple($registration_ids, $message) { | |
$fields = array( | |
'to' => $registration_ids, | |
//'data' => $message, | |
'data' => array("message" => $message) | |
); | |
return $this->sendPushNotification($fields); | |
} | |
// function makes curl request to firebase servers | |
private function sendPushNotification($fields) { | |
require_once 'config.php'; | |
// Set POST variables | |
$url = 'https://fcm.googleapis.com/fcm/send'; | |
$headers = array( | |
'Authorization: key=' . FIREBASE_API_KEY, | |
'Content-Type: application/json' | |
); | |
// Open connection | |
$ch = curl_init(); | |
// Set the url, number of POST vars, POST data | |
curl_setopt($ch, CURLOPT_URL, $url); | |
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)); | |
//$mehehe = json_encode($fields, JSON_PRETTY_PRINT); | |
//print($pretty_fields); | |
// Execute post | |
$result = curl_exec($ch); | |
if ($result === FALSE) { | |
die('Curl failed: ' . curl_error($ch)); | |
} | |
// Close connection | |
curl_close($ch); | |
return $result; | |
//return $mehehe; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment