Created
June 19, 2024 09:58
-
-
Save bastomiadi/caafb844e9648eb57b8862a2a941d0e8 to your computer and use it in GitHub Desktop.
FCM Firebase V1 Example With PHP Backend
This file contains 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 | |
function getAccessToken($serviceAccountPath) { | |
$now = time(); | |
$payload = [ | |
"iss" => $serviceAccountPath['client_email'], | |
"sub" => $serviceAccountPath['client_email'], | |
"aud" => "https://oauth2.googleapis.com/token", | |
"iat" => $now, | |
"exp" => $now + 3600, | |
"scope" => "https://www.googleapis.com/auth/firebase.messaging" | |
]; | |
$jwt = jwt_encode($payload, $serviceAccountPath['private_key'], 'sha256'); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, "https://oauth2.googleapis.com/token"); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/x-www-form-urlencoded"]); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([ | |
"grant_type" => "urn:ietf:params:oauth:grant-type:jwt-bearer", | |
"assertion" => $jwt | |
])); | |
$response = curl_exec($ch); | |
curl_close($ch); | |
$tokenInfo = json_decode($response, true); | |
return $tokenInfo['access_token']; | |
} | |
function jwt_encode($payload, $privateKey, $algo) { | |
$header = json_encode(["alg" => 'RS256', "typ" => "JWT"]); | |
$segments = []; | |
$segments[] = rtrim(strtr(base64_encode($header), '+/', '-_'), '='); | |
$segments[] = rtrim(strtr(base64_encode(json_encode($payload)), '+/', '-_'), '='); | |
$signingInput = implode('.', $segments); | |
$signature = sign($signingInput, $privateKey, $algo); | |
$segments[] = rtrim(strtr(base64_encode($signature), '+/', '-_'), '='); | |
return implode('.', $segments); | |
} | |
function sign($data, $key, $algo) { | |
openssl_sign($data, $signature, $key, $algo); | |
return $signature; | |
} | |
function sendFCMNotification($accessToken, $projectId, $message) { | |
$url = "https://fcm.googleapis.com/v1/projects/$projectId/messages:send"; | |
$headers = [ | |
"Authorization: Bearer $accessToken", | |
"Content-Type: application/json" | |
]; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(["message" => $message])); | |
$response = curl_exec($ch); | |
curl_close($ch); | |
return json_decode($response, true); | |
} | |
$credential = '{ | |
"type": "service_account", | |
"project_id": "xxx", | |
"private_key_id": "xxx", | |
"private_key": "-----BEGIN PRIVATE KEY-----\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n-----END PRIVATE KEY-----\n", | |
"client_email": "xxx", | |
"client_id": "xxx", | |
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | |
"token_uri": "https://oauth2.googleapis.com/token", | |
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | |
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/xxxxx", | |
"universe_domain": "googleapis.com" | |
}'; | |
// Example usage | |
$serviceAccountPath = json_decode($credential, true); | |
$accessToken = getAccessToken($serviceAccountPath); | |
$projectId = $serviceAccountPath['project_id']; | |
$message = [ | |
"token" => "fzyUqeOlR8-3xnIPlzZYD9:APA91bFbpj_B0XWTm08uzdscvYOVCrZ_4e60eOMOxN_G0WoFX3JveG1d5ZaZ2H_rB69TL4vP99IL2la6amjPDZM2fY474sAzoXMFi908VsLevsVoccueis8wrM_1wRTnsYiEyuFxMdAk", | |
"notification" => [ | |
"title" => "Hello", | |
"body" => "World" | |
] | |
]; | |
$response = sendFCMNotification($accessToken, $projectId, $message); | |
print_r($response); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment