简体   繁体   English

如何在 php 中使用 FCM HTTP v1 API

[英]How to use the FCM HTTP v1 API with php

I have used FCM with the legacy protocol but cannot find any concrete documentation to use the new FCM HTTP v1 API with php.我已经将 FCM 与旧协议一起使用,但找不到任何具体的文档来将新的 FCM HTTP v1 API 与 php 结合使用。

I have managed to import the Google API Client Library into my project but cannot find any documentation or tutorials on how to obtain the access token for the required scopes for fcm messages.我已设法将Google API 客户端库导入到我的项目中,但找不到有关如何获取 fcm 消息所需范围的访问令牌的任何文档或教程。

Maybe a bit late, but this is how to retrieve an oath token to be used with the FCM HTTP v1 API.可能有点晚了,但这是检索要与 FCM HTTP v1 API 一起使用的誓言令牌的方法。

  • Download this Google library to use it in your php code.下载此Google 库以在您的 php 代码中使用它。
  • Generate and download a new private key from the Firebase consoleFirebase 控制台生成并下载新的私钥
  • Store this key in json format in a secure place on your server.将此密钥以 json 格式存储在服务器上的安全位置。

How to configure the Google Client with your private key如何使用您的私钥配置 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
    }
}

How to request a new oauth token如何请求新的 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;
}

Please note that saveFile() and readFile() methods should be implemented as you prefer to store and retrieve the oath token json.请注意,应该实现saveFile()readFile()方法,因为您更喜欢存储和检索誓言令牌 json。 Follow the migration guide for the payload structure.遵循有效负载结构的迁移指南

If you are willing to use an existing library instead of implementing it yourself, you might consider having a look at https://github.com/kreait/firebase-php/ which has received support for FCM just today.如果您愿意使用现有的库而不是自己实现它,您可以考虑查看https://github.com/kreait/firebase-php/ ,它今天刚刚获得了对 FCM 的支持。

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

If it isn't for you, you will at least be able to extract the connections to the FCM REST API with PHP from the source code.如果它不适合您,您至少可以从源代码中使用 PHP 提取到 FCM REST API 的连接。 In short, it's the implementation of https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages .简而言之,它是https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages的实现。

Following the author's lead, here's an example php project that uses FCM HTTP v1 API:在作者的带领下,这里有一个使用 FCM HTTP v1 API 的示例 php 项目:

repo: https://github.com/jeromegamez/firebase-php-examples回购: https : //github.com/jeromegamez/firebase-php-examples
package docs: https://firebase-php.readthedocs.io/en/latest/cloud-messaging.html包文档: https : //firebase-php.readthedocs.io/en/latest/cloud-messaging.html
package repo: https://github.com/kreait/firebase-php包仓库: https : //github.com/kreait/firebase-php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FCM HTTP v1 批量请求在 PHP 中不起作用 - FCM HTTP v1 Batch request won't work in PHP 如何使用 v1 API 将 FCM 通知发送到特定设备令牌列表 - How to send FCM Notification to a list of specific device tokens using the v1 API .htaccess RewriteRule api / v1 /(.*)$ api / v1 / api.php?request = $ 1 [QSA,NC,L]不起作用 - .htaccess RewriteRule api/v1/(.*)$ api/v1/api.php?request=$1 [QSA,NC,L] not working Firebase云消息传递HTTP V1 API:如何使用REST调用获取Auth 2.0访问令牌? - Firebase Cloud Messaging HTTP V1 API: How to Get the Auth 2.0 Access Token with REST Calls? 如何验证Oauth v1和PHP - How to authenticate Oauth v1 and PHP PHP如何保存api.qrserver.com/v1/create-qr-code中生成的二维码 - PHP how to save qr code generated in api.qrserver.com/v1/create-qr-code 使用 v1 分支安装 google-api-php-client - Install google-api-php-client using the v1 branch 如何在magento api V1中检索客户订单 - how to retrive customer orders in magento api V1 如何按 coinmarketcap api v1 按美元价格对响应进行排序? - How to sort response by coinmarketcap api v1 by price in usd? 如何使用ionic v1将数据从angularjs发送到php? - How to send datas from angularjs to php using ionic v1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM