Skip to content

Instantly share code, notes, and snippets.

@YohannesTz
Created April 7, 2024 14:43
Show Gist options
  • Save YohannesTz/f57292731b92e04258f3537f95a78670 to your computer and use it in GitHub Desktop.
Save YohannesTz/f57292731b92e04258f3537f95a78670 to your computer and use it in GitHub Desktop.
FCM utility for PHP.
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Auth_Util {
private $firebaseConfig;
private $scope;
public function __construct() {
$this->CI =& get_instance();
$this->CI->load->library('FirebaseServiceConfig');
$this->firebaseConfig = new FirebaseServiceConfig();
//can be changed to another scope too
// refer this for more info: https://developers.google.com/identity/protocols/oauth2/scopes
$this->scope = "https://www.googleapis.com/auth/firebase.messaging";
}
public function generateAccessToken() {
$jwtToken = $this->createJwtToken();
$data = array(
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion' => $jwtToken
);
$ch = curl_init('https://www.googleapis.com/oauth2/v4/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
curl_close($ch);
$jsonResponse = json_decode($response, true);
return $jsonResponse['access_token'];
}
private function createJwtToken() {
$validForSec = 3600;
$privateKey = $this->firebaseConfig->private_key;
$saEmail = $this->firebaseConfig->client_email;
$header = json_encode(array("alg" => "RS256", "typ" => "JWT"));
$claim = json_encode(array(
"iss" => $saEmail,
"scope" => $this->scope,
"aud" => "https://www.googleapis.com/oauth2/v4/token",
"exp" => (time() + $validForSec),
"iat" => time()
));
$requestBody = base64_encode($header) . '.' . base64_encode($claim);
openssl_sign($requestBody, $signature, $privateKey, OPENSSL_ALGO_SHA256);
$signature = base64_encode($signature);
return $requestBody . '.' . $signature;
}
}
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class FirebaseServiceConfig {
public $type;
public $project_id;
public $private_key_id;
public $private_key;
public $client_email;
public $client_id;
public $auth_uri;
public $token_uri;
public $auth_provider_x509_cert_url;
public $client_x509_cert_url;
public $universe_domain;
//replace those with the real one from your .json file
public function __construct() {
$this->type = "service_account";
$this->project_id = "project_id";
$this->private_key_id = "blahblah";
$this->private_key = "-----BEGIN PRIVATE KEY-----\nblahblah\n-----END PRIVATE KEY-----\n";
$this->client_email = "";
$this->client_id = "";
$this->auth_uri = "";
$this->token_uri = "";
$this->auth_provider_x509_cert_url = "";
$this->client_x509_cert_uprivate = "";
$this->universe_domain = "googleapis.com";
}
}
<?php
class Some_Helper_class {
private function sendNotification($data, $headers)
{
$curl = curl_init();
//don't forget to change to the correct project-id
curl_setopt_array($curl, array(
CURLOPT_URL => "https://fcm.googleapis.com/v1/projects/project-id/messages:send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => $headers,
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
log_message('info', 'response: ' . $response);
if ($err) {
return "cURL Error #:" . $err;
} else {
return $response;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment