📢 *Send Notifications with Firebase Cloud Messaging in PHP*
In this step-by-step tutorial, you'll learn how to send Firebase Cloud Messaging (FCM) push notifications using PHP with secure token-based authentication. This guide replaces the deprecated server key method with the latest service account approach. You'll learn how to set up your Firebase project, download the private key JSON, use Composer to install Google APIs Client Library, generate access tokens securely, and send push notifications to your client devices. By the end of this guide, you'll have a modern and secure FCM integration in your PHP backend.
🚀 *FirebaseService*
The FirebaseService class provides functionality to send notifications to devices using Firebase Cloud Messaging (FCM). It uses the Google Client library for authentication and the FCM v1 API for sending messages.
🛠️ *Installation*
To use this project, you need to install the required dependencies. Run the following command:
```
composer require google/apiclient
```
⚙️ *Configuration*
Ensure you replace `your-firebase-project-id` in the FirebaseService.php file with your actual Firebase project ID. This applies to both the Admin SDK configuration file path and the FCM API URL.
🔑 *Firebase Admin SDK Configuration*
The FirebaseService.php file requires a service account JSON file for authentication. You can download this file from your Firebase Console under the Service Accounts section. Replace the placeholder path:
Make sure the file is placed in the correct directory.
📂 *Example File*
An example file `example.php` has been added to demonstrate how to use the FirebaseService class. You can run it as follows:
```
php example.php
```
📖 *Methods*
*sendNotification*
Parameters:
title: Title of the notification
message: Body of the notification
token: Firebase device token to send the notification to
data: Custom data payload (optional)
imageUrl: URL of the image to include in the notification (optional)
Returns:
Response from Firebase or an error message
Example on readme file
GitHub Repository:
[https://github.com/fayyaztech/fcm-lib...](https://github.com/fayyaztech/fcm-lib...)
Tags:
\#Firebase #FCM #PHPPushNotifications #TokenBasedAuthentication #FirebaseServiceAccount #GoogleClient #FCMIntegration #FirebaseMessaging #ComposerPHP #PHPTutorial #SecureNotifications