<?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);
?>
致命错误:未捕获的 GuzzleHttp\Exception\ClientException:客户端错误:`POST https://oauth2.googleapis.com/token\` 导致 `400 Bad Request` 响应:{"error":"invalid_grant", "error_description":"无效的 JWT:令牌必须是短期令牌(60 分钟)且在合理的时间范围内。检查 JWT 声明中的 iat 和 exp 值。"} 在 /var/www/html/*** 中**/''''''/google-api-php-client--PHP7.4/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:113 堆栈跟踪:#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- /var/www/html/*****/''''''/google-api-php-client--PHP7.4/vendor/guzzlehttp/ 中的客户端--PHP7.4/vendor/guzzlehttp/pr guzzle/src/Exception/RequestException.php 第 113 行
我正在尝试将我的 FCM 从旧版迁移到 http v1。我已经按照此处的文档进行操作,https://firebase.google.com/docs/cloud-messaging/migrate-v1,下载了 Firebase 管理 SDK 并安装了适用于 PHP 7.4 的 google API (https://github.com /googleapis/google-api-php-client)。然而,它没有按预期工作,我已经尝试了几天才能使其工作。有人可以指出我在这里做错了什么吗?
顺便说一下,我需要从网络向用户 Android 设备发送通知。生成的设备令牌并保存在数据库中。我还在学习中,所以请耐心等待。
我正在与您分享我如何解决这个问题。
我在这个演示中使用 Laravel 10
首先你需要使用composer安装适用于PHP的Google Auth Library
composer require google/auth
生成您的 Firebase 帐户 Json 凭据:复制您下载的 Json 或 Firebase 帐户常规设置中的 Firebase 项目 ID 将您的 json 凭证保存在项目中安全的地方。
现在使用此代码片段并根据您的用途进行调整
<?php
namespace App\CustomClass;
use App\Models\Configuration;
use Google\Auth\Credentials\ServiceAccountCredentials;
use Google\Auth\HttpHandler\HttpHandlerFactory;
class FireBasePushNotification
{
private $url = 'https://fcm.googleapis.com/v1/projects/<PROJECT_ID_HERE>/messages:send';
private $scope = "https://www.googleapis.com/auth/firebase.messaging";
private $token;
public function __construct()
{
$config = Configuration::first(); // I stored my json token in database
// Provide the path where you stored the json token, in my case, I stored it in database
$creadentials = new ServiceAccountCredentials($this->scope, $config->firebase_service);
$this->token = $creadentials->fetchAuthToken(HttpHandlerFactory::build());
}
public function to($device, $body, $title = "My favorite App")
{
$data = [
'token' => $device,
'title' => $title,
'body' => $body
];
return $this->send($data);
}
public function send($data)
{
$headers = [
'Authorization: Bearer ' . $this->token['access_token'],
'Content-Type: application/json'
];
$fields = [
'message' => [
'token' => $data['token'],
'notification' => [
'title' => $data['title'],
'body' => $data['body']
]
]
];
$fields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}我希望这可以帮助你,同时也帮助其他人