Skip to content

Instantly share code, notes, and snippets.

@sakilimran
Created April 23, 2019 07:12
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/9f9afd367444ed9c884a27ba9132bad9 to your computer and use it in GitHub Desktop.
Save sakilimran/9f9afd367444ed9c884a27ba9132bad9 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
{
public function subscribe_to_topic($topic_name)
{
$registration_tokens = ['fA3c_wLqvvs:APA91bHpmnkV8vkIPMJ4S6CxN_boB1dXvq2hzlkq0aRQ5FLnznPxpBMGTCpNTfz92sWvUADJ15D0aVPdHJv7ryI7pVLOv-wRMYid9jc1T4F9MrkLM8dZn7J-1ByF3LqqlnNp88iWHz3B'];
$fields = array(
"to" => "/topics/" . $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 unsubscribe_from_topic($topic_name)
{
$registration_tokens = ['fA3c_wLqvvs:APA91bHpmnkV8vkIPMJ4S6CxN_boB1dXvq2hzlkq0aRQ5FLnznPxpBMGTCpNTfz92sWvUADJ15D0aVPdHJv7ryI7pVLOv-wRMYid9jc1T4F9MrkLM8dZn7J-1ByF3LqqlnNp88iWHz3B'];
$fields = array(
"to" => "/topics/" . $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:batchRemove');
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 getDeviceDetails($registration_token)
{
$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/'.$registration_token.'?details=true');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
}
}
$push_notification = new FCM();
$push_notification->subscribe_to_topic('movies');
$push_notification->unsubscribe_from_topic('movies');
$push_notification->getDeviceDetails('dJzcCivdti0:APA91bHe3gY7axI2rOtQWei6TWbL8n3jz-hRZPRjQdVc0wcTGrSy-HWPP_ZabaVaDCTkV4EdjUTdYjgFlMx69-OMPXFZafdeSGZzuiUDG0LzuwpYGelpbLUtvmBRbd_4ZXGFytV8Mq-k');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment