Skip to content

Instantly share code, notes, and snippets.

@sakilimran
Last active April 9, 2019 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakilimran/1beb641e2d3d51598a625c17c95c00b6 to your computer and use it in GitHub Desktop.
Save sakilimran/1beb641e2d3d51598a625c17c95c00b6 to your computer and use it in GitHub Desktop.
<?php
// API access token from Google API's Console
define('ACCESS_TOKEN', 'ya29.GlvmBr9UH4KLL_Do6pHGOoZ7ksfURRHAqRh3dPWcMiYjNov4hhse_m147qlSwVZIqty989V73iQrtSD3DVT6AVaNtYFEQbQeq1EuUHhRMBTf1Go6lXTPPMzQX6-Q');
// API access key from Google API's Console
define('API_KEY', 'AIzaSyDB5KH7JaT5zRDq8QyRYOt3xMN4fyBaY_c');
// Firebase project id
define('PROJECT_ID', 'skitto-testbed');
class FCM
{
function __construct()
{
$this->topic_name = 'movies';
}
public function subscribe_to_topic()
{
$registration_tokens = ['fA3c_wLqvvs:APA91bHpmnkV8vkIPMJ4S6CxN_boB1dXvq2hzlkq0aRQ5FLnznPxpBMGTCpNTfz92sWvUADJ15D0aVPdHJv7ryI7pVLOv-wRMYid9jc1T4F9MrkLM8dZn7J-1ByF3LqqlnNp88iWHz3B'];
$fields = array(
"to" => "/topics/" . $this->topic_name,
"registration_tokens" => $registration_tokens
);
$headers = array
(
'Content-Type: application/json',
'Authorization: key=' . API_KEY
);
#Send Response To FireBase Server
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://iid.googleapis.com/iid/v1:batchAdd');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
}
public function send_messages_to_topic()
{
$fields = array
(
"message" => array(
"topic" => $this->topic_name,
"notification" => array(
"title" => "Background Message Title",
"body" => "Background message body"
)
)
);
$headers = array
(
'Content-Type: application/json',
'Authorization: bearer '.ACCESS_TOKEN
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com//v1/projects/'.PROJECT_ID.'/messages:send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
}
}
$push_notification = new FCM();
$push_notification->subscribe_to_topic();
$push_notification->send_messages_to_topic();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment