Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CrastyCrap's avatar

Laravel firebase

Can i push notifcation event from laravel api to firebase

0 likes
1 reply
LaryAI's avatar
Level 58

Yes, you can push notifications from a Laravel API to Firebase using the Firebase Cloud Messaging (FCM) service. Below is a step-by-step guide on how to set this up.

Step 1: Set Up Firebase in Your Project

  1. Go to the Firebase Console (https://console.firebase.google.com/).
  2. Create a new project or select an existing one.
  3. Navigate to the "Project settings" and go to the "Cloud Messaging" tab.
  4. Here, you'll find your Server key and Sender ID. Keep these handy as you'll need them to configure your Laravel application.

Step 2: Install Laravel FCM Package

You can use a package like laravel-fcm to simplify the integration. Install it via Composer:

composer require brozot/laravel-fcm

Step 3: Configure Laravel

After installing the package, publish the configuration file:

php artisan vendor:publish --provider="LaravelFCM\FCMServiceProvider"

This will create a config file config/fcm.php. Edit this file to include your Firebase server key:

return [
    'driver' => env('FCM_PROTOCOL', 'http'),
    'log_enabled' => false,

    'http' => [
        'server_key' => env('FCM_SERVER_KEY', 'your-server-key-here'),
        'sender_id' => env('FCM_SENDER_ID', 'your-sender-id-here'),
        'server_send_url' => 'https://fcm.googleapis.com/fcm/send',
        'server_group_url' => 'https://android.googleapis.com/gcm/notification',
        'timeout' => 30.0, // in second
    ],
];

Make sure to replace 'your-server-key-here' and 'your-sender-id-here' with the actual server key and sender ID from Firebase.

Step 4: Send a Notification

You can now send a notification from your Laravel application. Here’s an example of how to send a notification to a device:

use LaravelFCM\Message\OptionsBuilder;
use LaravelFCM\Message\PayloadDataBuilder;
use LaravelFCM\Message\PayloadNotificationBuilder;
use FCM;

function sendNotification($device_token, $title, $body) {
    $optionBuilder = new OptionsBuilder();
    $optionBuilder->setTimeToLive(60*20);

    $notificationBuilder = new PayloadNotificationBuilder($title);
    $notificationBuilder->setBody($body)
                        ->setSound('default');

    $dataBuilder = new PayloadDataBuilder();
    $dataBuilder->addData(['a_data' => 'my_data']);

    $option = $optionBuilder->build();
    $notification = $notificationBuilder->build();
    $data = $dataBuilder->build();

    $token = $device_token;

    $downstreamResponse = FCM::sendTo($token, $option, $notification, $data);

    return $downstreamResponse->numberSuccess();
}

Step 5: Test Your Notification

You can test sending a notification by calling the sendNotification function with appropriate parameters.

This setup allows your Laravel API to communicate with Firebase and send notifications to your users' devices. Adjust the notification details as needed for your specific application requirements.

Please or to participate in this conversation.