firebase Migrate from legacy FCM APIs to HTTP v1

Mohamed Hamdy - Jul 7 - - Dev Community

update send notification
instal Packagist ( google/apiclient )

   https://packagist.org/packages/google/apiclient

** create class GoogleAccessTokenService and use this NotificationController
firebase -> Project settings -> general -> Project ID
firebase -> Project settings -> service accounts -> generete new private key
file env (FIREBASE_CREDENTIALS="Project-name-firebase-adminsdk-71slz-90a88a5a5e.json")
file NotificationController

$firebaseApiUrl =
'https://fcm.googleapis.com/v1/projects/ Project ID /messages:send'

and See the display format for sending data in NotificationController

*get GoogleAccessToken = sourceKey
*

Unfortunately, the code is not available using php on the official website, but this is the code using PHP

<?php

namespace App\Services;

use Google\Client;
use Exception;

class GoogleAccessTokenService
{
    protected $client;

    public function __construct()
    {
        $this->client = new Client();
        $serviceAccountPath = base_path(env('FIREBASE_CREDENTIALS'));
        $this->client->setAuthConfig($serviceAccountPath);
        $this->client->addScope('https://www.googleapis.com/auth/firebase.messaging');
    }

    public function getAccessToken()
    {
        $accessToken = $this->client->fetchAccessTokenWithAssertion();

        if (isset($accessToken['access_token'])) {
            return $accessToken['access_token'];
        } else {
            throw new Exception('Failed to get access token');
        }
    }

} // end of GoogleAccessTokenService

Enter fullscreen mode Exit fullscreen mode

It also became a form

$header = [
            'Authorization' => 'Bearer' . $this->googleAccessToken,
            'Content-Type' => 'application/json',
        ];
Enter fullscreen mode Exit fullscreen mode
$payload = [
                    'message' => [
                        'token' => $androidUser->fcm_token,
                        'notification' => [
                            'title' => $title,
                            'body' => $body,
                        ],
                        "android"=> [
                          "priority" => "high",
                          "notification" => [
                            "click_action" => "TOP_STORY_ACTIVITY",
                            "sound" => "default",
                            // "image" => "https://foo.bar/pizza-monster.png",
                          ],
                          "data" => $data // البيانات الإضافية لتطبيق Android

                        ],
                        "apns" => [
                            "headers" => [
                                "apns-priority" => "10",
                            ],
                            "payload" => [
                                "aps" => [
                                    "mutable-content" => 1,
                                    "content-available" => 1,
                                    'sound' => 'default'
                                ]
                            ],
                            "fcm_options" =>  [
                                "image" => "https://foo.bar/pizza-monster.png",
                            ]
                        ],
                        'data' => $data
                    ]
                ];
Enter fullscreen mode Exit fullscreen mode
        $client = new Client(['headers' => $header]);

Enter fullscreen mode Exit fullscreen mode
 $res = $client->post($firebaseApiUrl, [
                    'json' => $payload,
                ]);
Enter fullscreen mode Exit fullscreen mode
.