Firebase Admin SDK for PHP

Created At: 2023-02-06 03:55:06 Updated At: 2023-02-07 07:22:43

Here we will see how to integrate Firebase service for PHP Laravel Framework for sending notification.

In order to access a Firebase project and its related services using a server SDK, requests must be authenticated. For server-to-server communication this is done with a Service Account.

The package uses auto discovery for the default project to find the credentials needed for authenticating requests to the Firebase APIs by inspecting certain environment variables and looking into Google's well known path(s).

Install & Publish

You need to get the package by running the following commnad. It would install PHP Laravel package for Firebase communication.

composer require kreait/laravel-firebase

Once this is done, you need to publish the package so that, Laravel framework can find it and use it.

# Laravel
php artisan vendor:publish --provider="Kreait\Laravel\Firebase\ServiceProvider" --tag=config

Firebase Admin SDK

Now let's really install the Firebase Admin SDK in your app. First you have to get the SDK from Firebase console. 

Go to Service accounts tab and then click on Generate new private key Then put the downloaded JSON file into your project root folder.

Configure .env File

Now it's time to configure our .env file. There are two things to configure in the .env file. I need to add the below two items in .env file.

  1. FIREBASE_DATABASE_URL
  2. FIREBASE_CREDENTIALS

To get FIREBASE_DATABASE_URL go to firebase console and get the project ID. See the picture below

 

# You can find the database URL for your project at
# https://console.firebase.google.com/project/_/database
FIREBASE_DATABASE_URL=https://<your-project>.firebaseio.com

Here in your-project you need to put your project ID from firebase.

And after that we need to add FIREBASE_CREDENTIALS. For this one we just need to add the file name that we downloaded from firebase console. That's it.

Comment

Add Reviews

Recommended Posts

Latest Posts

Subscribe our newsletter