我已经将FCM与旧协议一起使用,但找不到任何具体文档来使用带有php的新FCM HTTP v1 API。
我已经设法将 Google API 客户端库导入我的项目,但找不到有关如何获取 fcm 消息所需范围的访问令牌的任何文档或教程。
也许有点晚了,但这是检索要与 FCM HTTP v1 API 一起使用的誓言令牌的方法。
- 下载这个谷歌库,在你的php代码中使用它。
- 从 Firebase 控制台生成并下载新的私钥
- 将此密钥以 json 格式存储在服务器上的安全位置。
如何使用您的私钥配置 Google 客户端
public function configureClient()
{
$client = new Google_Client();
try {
$client->setAuthConfig("include_your_private_json_key_path_here");
$client->addScope(Google_Service_FirebaseCloudMessaging::CLOUD_PLATFORM);
// retrieve the saved oauth token if it exists, you can save it on your database or in a secure place on your server
$savedTokenJson = $this->readFile();
if ($savedTokenJson != null) {
// the token exists, set it to the client and check if it's still valid
$client->setAccessToken($savedTokenJson);
if ($client->isAccessTokenExpired()) {
// the token is expired, generate a new token and set it to the client
$accessToken = $this->generateToken($client);
$client->setAccessToken($accessToken);
}
} else {
// the token doesn't exist, generate a new token and set it to the client
$accessToken = $this->generateToken($client);
$client->setAccessToken($accessToken);
}
$oauthToken = $accessToken["access_token"];
// the client is configured, now you can send the push notification using the $oauthToken.
} catch (Google_Exception $e) {
// handle exception
}
}
如何请求新的 oauth 令牌
private function generateToken($client)
{
$client->fetchAccessTokenWithAssertion();
$accessToken = $client->getAccessToken();
// save the oauth token json on your database or in a secure place on your server
$tokenJson = json_encode($accessToken);
$this->saveFile($tokenJson);
return $accessToken;
}
请注意,应实现saveFile()
和readFile()
方法,因为您希望存储和检索誓言令牌 json。按照有效负载结构的迁移指南进行操作。
如果您愿意使用现有的库而不是自己实现它,您可以考虑看看今天刚刚获得 FCM 支持的 https://github.com/kreait/firebase-php/。
https://firebase-php.readthedocs.io/en/latest/cloud-messaging.html
如果不适合您,您至少可以从源代码中提取与 FCM REST API 和 PHP 的连接。简而言之,这是 https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages 的实施。
2023 年 7 月更新
批处理消息传递终结点已弃用,将于 2024 年 7 月删除。建议使用标准 v1 api send 方法循环访问令牌:https://firebase.google.com/docs/cloud-messaging/send-message#send-a-batch-of-messages
以下是有关如何向单个设备发送通知的示例:
<?php
require_once '<Path_to_google-api-php-client>/vendor/autoload.php';
function getOATHToken()
{
$client = new Google_Client();
try {
$client->setAuthConfig("<path_to_your_private_json_key.json>");
$client->addScope(Google_Service_FirebaseCloudMessaging::CLOUD_PLATFORM);
$savedTokenJson = readSavedToken();
if ($savedTokenJson) {
// the token exists, set it to the client and check if it's still valid
$client->setAccessToken($savedTokenJson);
$accessToken = $savedTokenJson;
if ($client->isAccessTokenExpired()) {
// the token is expired, generate a new token and set it to the client
$accessToken = generateToken($client);
$client->setAccessToken($accessToken);
}
} else {
// the token doesn't exist, generate a new token and set it to the client
$accessToken = generateToken($client);
$client->setAccessToken($accessToken);
}
$oauthToken = $accessToken["access_token"];
return $oauthToken;
} catch (Google_Exception $e) {}
return false;
}
//Using a simple file to cache and read the toke, can store it in a databse also
function readSavedToken() {
$tk = @file_get_contents('token.cache');
if ($tk) return json_decode($tk, true); else return false;
}
function writeToken($tk) {
file_put_contents("token.cache",$tk);
}
function generateToken($client)
{
$client->fetchAccessTokenWithAssertion();
$accessToken = $client->getAccessToken();
$tokenJson = json_encode($accessToken);
writeToken($tokenJson);
return $accessToken;
}
// Finally
function sendNotification($token, $title, $body, $accessToken) {
$payload = ["message" => ["token" => $token, "notification"=>["title" => $title, "body"=> $body]]];
$postdata = json_encode($payload);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-Type: application/json' . "rnAuthorization: Bearer $accessToken",
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('https://fcm.googleapis.com/v1/projects/<your_firebase_project_id>/messages:send', false, $context);
echo "nn======RESPONSE======nn";
echo $result;
}
$token = "token-1"; //FCM Device token
$title = "Test from PHP";
$body = "This is a test from PHP";
sendNotification($token, $title, $body, getOATHToken());
?>
批量发送示例(现已弃用(
下面是一个完整的示例,介绍如何使用 Google API PHP 客户端,使用 FCM HTTP v1 api 向多个令牌发送推送,如 FCM 文档中所示。
(修改自上面的卢比利斯答案(
<?php
require_once '<Path_to_google-api-php-client>/vendor/autoload.php';
function getOATHToken()
{
$client = new Google_Client();
try {
$client->setAuthConfig("<path_to_your_private_json_key.json>");
$client->addScope(Google_Service_FirebaseCloudMessaging::CLOUD_PLATFORM);
$savedTokenJson = readSavedToken();
if ($savedTokenJson) {
// the token exists, set it to the client and check if it's still valid
$client->setAccessToken($savedTokenJson);
$accessToken = $savedTokenJson;
if ($client->isAccessTokenExpired()) {
// the token is expired, generate a new token and set it to the client
$accessToken = generateToken($client);
$client->setAccessToken($accessToken);
}
} else {
// the token doesn't exist, generate a new token and set it to the client
$accessToken = generateToken($client);
$client->setAccessToken($accessToken);
}
$oauthToken = $accessToken["access_token"];
return $oauthToken;
} catch (Google_Exception $e) {}
return false;
}
//Using a simple file to cache and read the toke, can store it in a databse also
function readSavedToken() {
$tk = @file_get_contents('token.cache');
if ($tk) return json_decode($tk, true); else return false;
}
function writeToken($tk) {
file_put_contents("token.cache",$tk);
}
function generateToken($client)
{
$client->fetchAccessTokenWithAssertion();
$accessToken = $client->getAccessToken();
$tokenJson = json_encode($accessToken);
writeToken($tokenJson);
return $accessToken;
}
// Finally
function sendNotification($tokens, $title, $body, $accessToken) {
$boundary = "--subrequest_boundary";
$multiPayload = $boundary;
foreach ($tokens as $token) {
$head = "Content-Type: application/httprn".
"Content-Transfer-Encoding: binaryrnrn".
"POST /v1/projects/<your_firebase_project_name>/messages:sendrn".
"Content-Type: application/jsonrn".
"accept: application/jsonrnrn";
$payload = ["message" => ["token" => $token, "notification"=>["title" => $title, "body"=> $body]]];
$postdata = json_encode($payload);
$multiPayload .= "rn".$head.$postdata."rn".$boundary;
}
$multiPayload .= "--";
//Uncomment for debugging
//echo "nn==== POST DATA====nn";
//echo $multiPayload;
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-Type: multipart/mixed; boundary="subrequest_boundary"' . "rnAuthorization: Bearer $accessToken",
'content' => $multiPayload
)
);
//Uncomment for debugging
//echo "nn==== HTTP OPTIONS====nn";
//var_dump($opts);
$context = stream_context_create($opts);
// This is the path for sending push multiple tokens (upto 500 as per the docs)
$result = file_get_contents('https://fcm.googleapis.com/batch', false, $context);
echo "nn======RESPONSE======nn";
echo $result;
}
$tokens = ["token-1","token-2"]; //FCM Device tokens as array
$title = "Test from PHP";
$body = "This is a test from PHP";
sendNotification($tokens, $title, $body, getOATHToken());
?>
按照作者的指导,下面是一个使用 FCM HTTP v1 API 的 php 项目示例:
回购:https://github.com/jeromegamez/firebase-php-examples
包文档:https://firebase-php.readthedocs.io/en/latest/cloud-messaging.html
包存储库:https://github.com/kreait/firebase-php