Larafirebase is a package thats offers you to send push notifications or custom messages via Firebase in Laravel.

Last update: Jun 28, 2022

Total Downloads Latest Stable Version License

Introduction

Larafirebase is a package thats offers you to send push notifications or custom messages via Firebase in Laravel.

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably deliver messages at no cost.

For use cases such as instant messaging, a message can transfer a payload of up to 4KB to a client app.

Installation

Follow the steps below to install the package.

Composer

composer require kutia-software-company/larafirebase

Copy Config

Run php artisan vendor:publish --provider="Kutia\Larafirebase\Providers\LarafirebaseServiceProvider" to publish the larafirebase.php config file.

Get Athentication Key

Get Authentication Key from https://console.firebase.google.com/

Configure larafirebase.php as needed

'authentication_key' => '{AUTHENTICATION_KEY}'

Usage

Follow the steps below to find how to use the package.

Example usage in Controller/Service or any class:

use Kutia\Larafirebase\Facades\Larafirebase;

class MyController
{
    private $deviceTokens =['{TOKEN_1}', '{TOKEN_2}'];

    public function sendNotification()
    {
        return Larafirebase::withTitle('Test Title')
            ->withBody('Test body')
            ->withImage('https://firebase.google.com/images/social.png')
            ->withClickAction('admin/notifications')
            ->withPriority('high')
            ->sendNotification($this->deviceTokens);
        
        // Or
        return Larafirebase::fromArray(['title' => 'Test Title', 'body' => 'Test body'])->sendNotification($this->deviceTokens);
    }

    public function sendMessage()
    {
        return Larafirebase::withTitle('Test Title')
            ->withBody('Test body')
            ->sendMessage($this->deviceTokens);
            
        // Or
        return Larafirebase::fromArray(['title' => 'Test Title', 'body' => 'Test body'])->sendMessage($this->deviceTokens);
    }
}

Example usage in Notification class:

use Illuminate\Notifications\Notification;
use Kutia\Larafirebase\Messages\FirebaseMessage;

class SendBirthdayReminder extends Notification
{
    /**
     * Get the notification's delivery channels.
     */
    public function via($notifiable)
    {
        return ['firebase'];
    }

    /**
     * Get the firebase representation of the notification.
     */
    public function toFirebase($notifiable)
    {
        $deviceTokens = [
            '{TOKEN_1}',
            '{TOKEN_2}'
        ];
        
        return (new FirebaseMessage)
            ->withTitle('Hey, ', $notifiable->first_name)
            ->withBody('Happy Birthday!')
            ->asNotification($deviceTokens); // OR ->asMessage($deviceTokens);
    }
}

Tips

  • Check example how to receive messages or push notifications in a JavaScript client.
  • You can use larafirebase() helper instead of Facade.

Payload

Check how is formed payload to send to firebase:

Example 1:

Larafirebase::withTitle('Test Title')->withBody('Test body')->sendNotification('token1');
{
  "registration_ids": [
    "token1"
  ],
  "notification": {
    "title": "Test Title",
    "body": "Test body"
  },
  "priority": "normal"
}

Example 2:

Larafirebase::withTitle('Test Title')->withBody('Test body')->sendMessage('token1');
{
  "registration_ids": [
    "token1"
  ],
  "data": {
    "title": "Test Title",
    "body": "Test body"
  },
  "priority": "normal"
}

If you want to create payload from scratch you can use method fromRaw, for example:

return Larafirebase::fromRaw([
    'registration_ids' => ['token1', 'token2'],
    'data' => [
        'key_1' => 'Value 1',
        'key_2' => 'Value 2'
    ],
    'android' => [
        'ttl' => '1000s',
        'priority' => 'normal',
        'notification' => [
            'key_1' => 'Value 1',
            'key_2' => 'Value 2'
        ],
    ],
])->send();

Made with by Gentrit Abazi (@gentritabazi01).

GitHub

https://github.com/kutia-software-company/larafirebase
Comments
  • 1. do fill data in js worker in require ?

        firebase.initializeApp({
            'messagingSenderId': '{MESSAGING_SENDER_ID}',
            'apiKey': '{API_KEY}',	
            'projectId': '{PROJECT_ID}',	
            'appId': '{APP_ID}'
        });
    
    Reviewed by vahidalvandi at 2021-08-23 08:51
  • 2. Class 'Illuminate\Support\Facades\Http' not found

    Hi guys. I'm getting this error:

    Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Class 'Illuminate\Support\Facades\Http' not found

    Laravel: 6.20.13

    In my controller:

    use Illuminate\Support\Facades\Http; use Kutia\Larafirebase\Facades\Larafirebase;

    Can someone help me please?

    Reviewed by thiagok at 2021-03-18 14:59
  • 3. How to pass optional data

    I want to send optional data like. ["url" => "https://www.google.com"] I don't see any option.

    Additionally, how can I get a downstream response for failed tokens?

    Reviewed by bluelupin at 2021-02-16 16:42
  • 4. Which is correct token format

    hi , do this coorect format ?

    ['{TOKEN_1}', '{TOKEN_2}']

    ["{sdfsd}","{fhsd}"]

    this is one real token and seny many like this to '{cXv99TFMHPwyMxy84UnXlf:APA91bErGR73C-69-SCsWugEiHXwSZHobu2V8UVIG_a_QnmJPbsAJFwZ5_XtlSiL4DFINvkL-Qlybh-6XDMCXMOIkUIPrg8Cd0_8KDbSUmymx1ZJWvYDxrPdg7zqippInSlyf62HCehT}'

    Reviewed by vahidalvandi at 2022-01-12 09:45
  • 5. How to find Authentication Key

    Very Good Package , Please i need to know what authentication token you're talking about in the config/larafirebase.php , cause i have been all over my google console and i can only find API keys not authentication key #

    Reviewed by Laozofficial at 2021-03-31 13:43
  • 6. Icon option doesn't work

    I tried using withIcon() method and key 'icon' on fromRaw() method, I've also tried to use different file formats, such as .png, .svg and .xml but it doesn't work for me at all.

    Reviewed by otavioq at 2021-11-16 19:46
  • 7. Driver [firebase] not supported

    Hi!

    Error on send notifications for me

    Laravel 8x

    class SendNewStarNotification extends Notification implements ShouldQueue
    {
        use Queueable;
    
        public function __construct()
        {
            //
        }
    
        public function via($notifiable)
        {
            return ['firebase'];
        }
    
        public function toFirebase($notifiable)
        {
            if ($notifiable->firebase_unique_token) {
                $deviceTokens = [$notifiable->firebase_unique_token];
                return (new FirebaseMessage)
                    ->withTitle('Ola, ', $notifiable->name)
                    ->withBody('Teste do maluko')
                    ->asNotification($deviceTokens); // OR ->asMessage($deviceTokens);
            }
        }
    }
    

    On send, in horizon:

    InvalidArgumentException: Driver [firebase] not supported. in /var/www/app/vendor/laravel/framework/src/Illuminate/Support/Manager.php:109

    Reviewed by blitsoftwares at 2021-09-24 23:14
  • 8. withClickAction method

    Hello,

    I assume for sure this is not an issue, and i will be glad if any one guide me through this..

    I'm using this great package, and i wonder how i can route my pages (Flutter - Getx) in case a notification tapped

    Knowing that i'm passing an argument to withClickAction : ->withClickAction('/expressClue')

    Using flutter..

    Thank you.

    Reviewed by YassineChe at 2021-08-24 00:06
  • 9. Ability to set notification sound?

    To set the notification sound, the Firebase Cloud Messaging payload must have a notification.sound key.

    Supports default or the filename of a sound resource bundled in the app.

    Referenced in this StackOverflow post.

    Let's add the ability to add a sound key.

    Reviewed by exzachlyvv at 2021-05-19 00:21
  • 10. Added support for setting badge count on iOS

    This PR allows the user to attach a badge count, in other words, the number of unread notifications on an app icon. It also allows sending background updates through Firebase, e.g. to wake up the app from a sleeping state.

    Reviewed by lagerroos at 2022-05-09 10:30
  • 11. A helper method for adding notification sound?

    Would you consider adding a helper method to define the notification sound, the only way to achieve this at the moment is to use the fromRaw or fromArray methods which is okay but you lose all the nice helper methods.

    Without using this option all notifications currently come through without sound and vibrations.

    Something like:

    // Kutia\Larafirebase\Services\Larafirebase.php
    
    private $sound;
    
    ...
    
    public function withSound($sound)
    {
        $this->sound = $sound;
    
        return $this;
    }
    
    ...
    
    public function sendNotification($tokens)
    {
        $fields = array(
            'registration_ids' => $this->validateToken($tokens),
            'notification' => ($this->fromArray) ? $this->fromArray : [
                'title' => $this->title,
                'body' => $this->body,
                'image' => $this->image,
                'icon' => $this->icon,
                'sound' => $this->sound,
                'click_action' => $this->clickAction
            ],
            'data' => $this->additionalData,
            'priority' => $this->priority
        );
        return $this->callApi($fields);
    }
    
    Reviewed by CharlieHorton at 2022-03-10 23:11
This package makes it easy to send web push notifications with Laravel.

Web push notifications channel for Laravel This package makes it easy to send web push notifications with Laravel. Installation You can install the pa

Jun 30, 2022
A PHP Library to easily send push notifications with the Pushwoosh REST Web Services.

php-pushwoosh A PHP Library to easily send push notifications with the Pushwoosh REST Web Services. First sample, creating a Pushwoosh message // Crea

Jun 17, 2022
Send push notifications to apple devices (iPhone, iPad, iPod).

Apple Apn Push Send push notifications to apple devices (iPhone, iPad, iPod). Support authenticators: Certificate Json Web Token Supported protocols:

Apr 26, 2022
WebPush can be used to send notifications to endpoints which server delivers Web Push

WebPush can be used to send notifications to endpoints which server delivers Web Push notifications as described in the Web Push protocol. As it is standardized, you don't have to worry about what server type it relies on.

Jun 20, 2022
Laravel package to enable sending push notifications to devices

Laravel Push Notification Package to enable sending push notifications to devices Installation Update your composer.json file to include this package

Jun 16, 2022
This package allows you to send notifications to Microsoft Teams.
This package allows you to send notifications to Microsoft Teams.

Teams connector This package allows you to send notifications to Microsoft Teams. Installation You can install the package using the Composer package

May 6, 2022
Push Notifications using Laravel
Push Notifications using Laravel

laravel-push-notification Push Notifications using Laravel PushNotification::send(['deviceToken1', 'deviceToken2',..], 'Notification Message', 'Action

Apr 6, 2022
Standalone PHP library for easy devices notifications push.

NotificationPusher Standalone PHP library for easy devices message notifications push. Feel free to contribute! Thanks. Contributors Cédric Dugat (Aut

Jun 15, 2022
Takes care of Apple push notifications (APNS) in your PHP projects.

Notificato Notificato takes care of push notifications in your PHP projects. Italian: notificato è: participio passato English: notified Why use Notif

Jun 27, 2022
Push notifications Library for PHP

Push notifications Library for PHP Supported Protocols Protocol Supported Driver Options APNs (Token Based) ✓ APNs\Token APNs\Token\Option APNs (Certi

Mar 25, 2022
Takes care of Apple push notifications (APNS) in your PHP projects.

Notificato Notificato takes care of push notifications in your PHP projects. Italian: notificato è: participio passato English: notified Why use Notif

Apr 17, 2022
Standalone PHP library for easy devices notifications push.

NotificationPusher Standalone PHP library for easy devices message notifications push. Feel free to contribute! Thanks. Contributors Cédric Dugat (Aut

Jun 15, 2022
This package makes it easy to send notifications using RocketChat with Laravel 9.0+.

laravel-rocket-chat-notifications Introduction This package makes it easy to send notifications using RocketChat with Laravel 9.0+. Contents Installat

May 27, 2022
Laravel SMS allows you to send SMS from your Laravel application using multiple sms providers, allow to add custom sms provider

Laravel SMS Laravel SMS allows you to send SMS from your Laravel application using multiple sms providers, allow to add custom sms provider Requiremen

May 7, 2022
:computer: Send notifications to your desktop directly from your PHP script
:computer: Send notifications to your desktop directly from your PHP script

About JoliNotif JoliNotif is a cross-platform PHP library to display desktop notifications. It works on Linux, Windows or MacOS. Requires PHP >= 7.2 (

Jun 26, 2022
Notifications in PHP (notify-send, growl, etc) like that.

#Nod Notifications in PHP (notify-send, growl, etc) like that. ##Examples Letting Nod figure out the best Adapter to use (not recommend ATM, only work

Mar 26, 2019
Sends notifications via one or more channels (email, SMS, ...).

Notifier Component The Notifier component sends notifications via one or more channels (email, SMS, ...). Resources Documentation Contributing Report

Jun 27, 2022
Laravel FCM (Firebase Cloud Messaging) Notification Channel

Laravel FCM Notification Laravel FCM (Firebase Cloud Messaging) Notification Channel Use this package to send push notifications via Laravel to Fireba

Jun 30, 2022
ApnsPHP: Apple Push Notification & Feedback Provider
ApnsPHP: Apple Push Notification & Feedback Provider

ApnsPHP: Apple Push Notification & Feedback Provider A full set of open source PHP classes to interact with the Apple Push Notification service for th

Jun 27, 2022