[英]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 一起使用的誓言令牌的方法。
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
}
}
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.