如何在 php 中使用 FCM HTTP v1 API

Jud*_*des 7 php firebase firebase-cloud-messaging

我已经将 FCM 与旧协议一起使用,但找不到任何具体的文档来将新的 FCM HTTP v1 API 与 php 结合使用。

我已设法将Google API 客户端库导入到我的项目中,但找不到有关如何获取 fcm 消息所需范围的访问令牌的任何文档或教程。

Moh*_*one 10

2023 年 7 月更新

批量消息端点已被弃用,并将于 2024 年 7 月删除。建议使用标准 v1 api 发送方法迭代令牌:https ://firebase.google.com/docs/cloud-messaging/send-message #发送一批消息

以下是如何向单个设备发送通知的示例:

<?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' . "\r\nAuthorization: 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 "\n\n======RESPONSE======\n\n";
    echo $result;

}

$token = "token-1"; //FCM Device token

$title = "Test from PHP";
$body = "This is a test from PHP";

sendNotification($token, $title, $body, getOATHToken());

?>
Run Code Online (Sandbox Code Playgroud)

批量发送示例(现已弃用)

以下是关于如何使用Google API PHP Client和 FCM HTTP v1 api向多个令牌发送推送的完整示例,如FCM 文档中所示。

(根据上面lubilis的回答修改)

<?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/http\r\n".
                "Content-Transfer-Encoding: binary\r\n\r\n".
                "POST /v1/projects/<your_firebase_project_name>/messages:send\r\n".
                "Content-Type: application/json\r\n".
                "accept: application/json\r\n\r\n";
        
        $payload = ["message" => ["token" => $token, "notification"=>["title" => $title, "body"=> $body]]];

        $postdata = json_encode($payload);
        $multiPayload .= "\r\n".$head.$postdata."\r\n".$boundary;
        
    }
    
    $multiPayload .= "--";
    

    //Uncomment for debugging
    //echo "\n\n==== POST DATA====\n\n";
    //echo $multiPayload;


    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-Type: multipart/mixed; boundary="subrequest_boundary"' . "\r\nAuthorization: Bearer $accessToken",
            'content' => $multiPayload
        )
    );

    //Uncomment for debugging
    //echo "\n\n==== HTTP OPTIONS====\n\n";
    //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 "\n\n======RESPONSE======\n\n";
    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());

?>
Run Code Online (Sandbox Code Playgroud)


lub*_*lis 8

可能有点晚了,但这是检索要与 FCM HTTP v1 API 一起使用的誓言令牌的方法。

  • 下载此Google 库以在您的 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
    }
}
Run Code Online (Sandbox Code Playgroud)

如何请求新的 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;
}
Run Code Online (Sandbox Code Playgroud)

请注意,当您更喜欢存储和检索誓言令牌 json 时,应该实现saveFile()readFile()方法。遵循有效负载结构的迁移指南

  • 如果您只想向应用程序发送通知,请使用此方法,使用 Laravel 库对于此任务来说只是过度杀戮,而且这种方法更容易使用。 (2认同)

jer*_*mez 6

如果您愿意使用现有的库而不是自己实现它,您可以考虑查看https://github.com/kreait/firebase-php/,它今天刚刚获得了对 FCM 的支持。

https://firebase-php.readthedocs.io/en/latest/cloud-messaging.html

如果它不适合您,您至少能够从源代码中使用 PHP 提取与 FCM REST API 的连接。简而言之,它是https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages的实现。