How to use the FCM HTTP v1 API with php

落花浮王杯 提交于 2020-06-25 21:16:53

问题


I have used FCM with the legacy protocol but cannot find any concrete documentation to use the new FCM HTTP v1 API with 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.


回答1:


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://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. In short, it's the implementation of https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages .




回答2:


Following the author's lead, here's an example php project that uses FCM HTTP v1 API:

repo: https://github.com/jeromegamez/firebase-php-examples
package docs: https://firebase-php.readthedocs.io/en/latest/cloud-messaging.html
package repo: https://github.com/kreait/firebase-php




回答3:


Maybe a bit late, but this is how to retrieve an oath token to be used with the FCM HTTP v1 API.

  • Download this Google library to use it in your php code.
  • Generate and download a new private key from the Firebase console
  • Store this key in json format in a secure place on your server.

How to configure the Google Client with your private key

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

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. Follow the migration guide for the payload structure.



来源:https://stackoverflow.com/questions/49782134/how-to-use-the-fcm-http-v1-api-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!