<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once 'google-api-php-client--PHP7.4/vendor/autoload.php';
$title = "title";
$body = "body";
$devicetoken = "*****device_token****";
function getGoogleAccessToken(){
$credentialsFilePath = 'service-account.json';
$client = new Google_Client();
$client->setAuthConfig($credentialsFilePath);
$client->addScope('https://www.googleapis.com/auth/firebase.messaging');
$client->refreshTokenWithAssertion();
$token = $client->getAccessToken();
return $token['access_token'];
}
function sendFCM($title, $body, $devicetoken) {
$url = 'https://fcm.googleapis.com/v1/projects/***project_name*****/messages:send';
$headers = array (
'Authorization: Bearer ' . getGoogleAccessToken(),
'Content-Type:application/json'
);
$notifData = [
'title' => $title,
'body' => $body,
'click_action' => "OPEN_NOTIFY_PAGE",
];
$apiBody = [
'notification' => $notifData,
'token' => $devicetoken];
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, json_encode($apiBody));
$result = curl_exec($ch);
print($result);
curl_close($ch);
return $result;
}
sendFCM($title, $body, $devicetoken);
?>
Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `POST https://oauth2.googleapis.com/token\` resulted in a `400 Bad Request` response: {"error":"invalid_grant","error_description":"Invalid JWT: Token must be a short-lived token (60 minutes) and in a reasonable timeframe. Check your iat and exp values in the JWT claim."} in /var/www/html/*****/''''''/google-api-php-client--PHP7.4/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113 Stack trace: #0 /var/www/html/*****/''''''/google-api-php-client--PHP7.4/vendor/guzzlehttp/guzzle/src/Middleware.php(69): GuzzleHttp\Exception\RequestException::create() #1 /var/www/html/*****/''''''/google-api-php-client--PHP7.4/vendor/guzzlehttp/promises/src/Promise.php(204): GuzzleHttp\Middleware::GuzzleHttp{closure}() #2 /var/www/html/*****/''''''/google-api-php-client--PHP7.4/vendor/guzzlehttp/promises/src/Promise.php(153): GuzzleHttp\Promise\Promise::callHandler() #3 /var/www/html/*****/''''''/google-api-php-client--PHP7.4/vendor/guzzlehttp/pr in /var/www/html/*****/''''''/google-api-php-client--PHP7.4/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 113
I am trying to migrate my FCM from the legacy to http v1. I already follow the docs here, https://firebase.google.com/docs/cloud-messaging/migrate-v1, downloaded the Firebase admin SDK and installed google API for PHP 7.4 (https://github.com/googleapis/google-api-php-client). However, its not working as expected and I have been trying days to make it working. Can someone point out what I did wrong here?
As a side note, I need to send notification from the web to the user android device. The device token generated and saved in the database. I am still learning so please bear with me.