How to Send Firebase Notifications with Laravel using the FCMA Package

Jun 27, 2022 | Programming

Firebase Cloud Messaging (FCM) is a powerful service that enables developers to send notifications and data messages to apps across platforms. In this guide, we will walk you through the process of using the Laravel FCMA package to deliver notifications seamlessly in your Laravel applications.

Installation

To get started with the Laravel FCMA package, you’ll need to install it via Composer. Here’s how:

bash
$ composer require kawankoding/laravel-fcm ^0.2.0

Setting Up in Laravel

After installing the package, you’ll need to register the service provider:

php
// config/app.php
'providers' => [
    ...
    Kawankoding\Fcm\FcmServiceProvider::class,
],

If you want to utilize the facade, make sure to add it as follows:

php
// config/app.php
'aliases' => [
    ...
    'Fcm' => Kawankoding\Fcm\FcmFacade::class,
],

Next, publish the configuration file to configure your FCM server key:

bash
php artisan vendor:publish --provider="Kawankoding\Fcm\FcmServiceProvider"

Configuring Your Server Key

Upon publishing, your configuration file will look something like this:

php
return [
    // Set your FCM Server Key
    // Change to yours
    'server_key' => env('FCM_SERVER_KEY', ''),
];

Make sure to define your FCM_SERVER_KEY in your .env file:

php
APP_NAME=Laravel
# ...
FCM_SERVER_KEY=putYourKeyHere

Setting Up in Lumen

If you’re using Lumen, add the service provider and configuration as follows:

php
// bootstrap/app.php
$app->register(Kawankoding\Fcm\FcmServiceProvider::class);

// Important: This needs to be before the registration of the service provider
$app->configure('laravel-fcm');

$app->register(Kawankoding\Fcm\FcmServiceProvider::class);

Methods Reference

The package provides several methods for sending notifications:

  • to()
  • toTopic()
  • data()
  • notification()
  • priority()
  • timeToLive()
  • enableResponseLog()
  • send()

Usage Examples

Let’s dive into how to actually send notifications using the package.

Sending a FCM with Data Parameter

To send a notification with just a data parameter, here’s an example:

php
$recipients = [
    'clKMv.......',
    'GxQQW.......',
];
fcm()
    ->to($recipients)
    ->priority('high')
    ->timeToLive(0)
    ->data([
        'title' => 'Test FCM',
        'body' => 'This is a test of FCM',
    ])
    ->send();

It’s important to note that by default, the Firebase server queues your notification for 4 weeks. You can alter this behavior with timeToLive(value_in_seconds). For instance, timeToLive(0) will ensure that the notification is delivered immediately, but only if the target device is online.

Sending a FCM to a Topic

To send a FCM to a specific topic, you’d use the toTopic($topic) method:

php
fcm()
    ->toTopic($topic) // $topic must be a string (topic name)
    ->priority('normal')
    ->timeToLive(0)
    ->notification([
        'title' => 'Test FCM',
        'body' => 'This is a test of FCM',
    ])
    ->send();

Sending a FCM with Both Data and Notification Parameters

If you want to send a notification with both data and notification parameters, you can do this:

php
fcm()
    ->to($recipients) // $recipients must be an array
    ->priority('normal')
    ->timeToLive(0)
    ->data([
        'title' => 'Test FCM',
        'body' => 'This is a test of FCM',
    ])
    ->notification([
        'title' => 'Test FCM',
        'body' => 'This is a test of FCM',
    ])
    ->send();

Logging Responses

To view the original response from Firebase, use the enableResponseLog() method:

php
fcm()
    ->to($recipients)
    ...
    ->enableResponseLog()
    ->send();

You can then check the response log in storage/logs/laravel.log.

Troubleshooting Ideas

  • Ensure that your FCM Server Key is correctly set in your configuration.
  • Check for any connectivity issues or firewall restrictions that might affect Firebase’s ability to send notifications.
  • Verify that the recipient device is registered and capable of receiving notifications.
  • Make sure the topic name is correctly formatted when using topics.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox