【问题标题】:How to use the FCM HTTP v1 API with php如何在 php 中使用 FCM HTTP v1 API
【发布时间】:2018-09-21 18:08:11
【问题描述】:

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

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

【问题讨论】:

  • 好吧,我不知道你在做什么,因为你没有编码,通过 FCM 发送消息是什么,你在 firebase 的同一个控制台中的项目配置中有 api 密钥,在另一方面,我离开了 unh repo,在其中我有一个表格可以通过 firebase 发送通知,查看它是否有帮助。 REPOSITORY
  • @CarlosQuintero 我查看了您的代码,您使用的是旧的发送 fcm 请求的旧方式,我已经得到了,我想知道如何使用 HTTP v1 API 为新方式编写代码php
  • 看到这个REPO
  • @CarlosQuintero 谢谢,这似乎应该有所帮助,一旦完成,将尝试并将您的标记为正确答案。

标签: php firebase firebase-cloud-messaging


【解决方案1】:

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

  • 下载此Google library 以在您的 php 代码中使用它。
  • Firebase console 生成并下载新的私钥
  • 将此密钥以 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。按照 migration guide 获取有效负载结构。

【讨论】:

  • 如果你只是想向应用发送通知,请使用这个,使用 laravel 库对于这个任务来说是一种过度杀戮,而且这种方法更容易使用。
【解决方案2】:

按照作者的介绍,下面是一个使用 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

【讨论】:

    【解决方案3】:

    如果您愿意使用现有库而不是自己实现它,您可以考虑查看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的实现。

    【讨论】:

    • 感谢您的参考,但在查看文档时,我发现了一个注释,上面写着 The Cloud Messaging API currently does not support sending messages tailored to different target platforms (Android, iOS and Web).,这只能在 HTTPv1 FCM 调用中使用,所以它看起来仍然只支持旧协议跨度>
    • 它不支持它,平台配置将在未来的版本中推出 - 我应该更清楚地说明:)
    • 期待,你是图书馆的作者吗?
    • 是的:)。我通常在引用它时写这个,只是我忘记了另一件事?
    • 我在搜索其他内容时偶然发现了这一点,想让您知道 FCM 现在已在 PHP SDK 中完全实现 \o/
    猜你喜欢
    • 1970-01-01
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    相关资源
    最近更新 更多