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

Overview

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).

Comments
  • do fill data in js worker in require ?

    do fill data in js worker in require ?

        firebase.initializeApp({
            'messagingSenderId': '{MESSAGING_SENDER_ID}',
            'apiKey': '{API_KEY}',	
            'projectId': '{PROJECT_ID}',	
            'appId': '{APP_ID}'
        });
    
    opened by vahidalvandi 5
  • Class 'Illuminate\Support\Facades\Http' not found

    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?

    opened by thiagok 5
  • Click_Action Not Working ?

    Click_Action Not Working ?

    Hi, ı working to send notification with this package but click action not working. Image,title,body OK but url not working.

    This my code : Larafirebase::withTitle('Example Title') ->withBody('Example Body') ->withClickAction('https://mysite.com/my-post-url') ->withImage('https://firebase.google.com/images/social.png') ->withIcon('https://firebase.google.com/images/social.png') ->withPriority('high') ->sendNotification($token);

    opened by benahmetcelik 4
  • How to pass optional data

    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?

    opened by bluelupin 4
  • [1.x] Add support for setting badge count on iOS

    [1.x] Add 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.

    opened by lagerroos 3
  • Which is correct token format

    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}'

    help wanted 
    opened by vahidalvandi 3
  • Send a badge property

    Send a badge property

    Send a badge property to set a number for unread notifications in iOS. This property should be sent in notification array but not in data array.

    There is a PR already for that but it is closed. I think it should be reopened and then accepted. https://github.com/kutia-software-company/larafirebase/pull/41

    Right now it works if we use fromArray method instead of withTitle, withBody and withAdditionalData methods. This is not very convenient.

    new-feature 
    opened by Artyomushko 2
  • How to find Authentication Key

    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 #

    help wanted 
    opened by Laozofficial 2
  • Notification lifespan/time to leave option

    Notification lifespan/time to leave option

    It could be good that we can add lifespan option to notifications we send over firebase. ttl-time to leave, lifespan of fcm message can be changed on way it is described in this link https://firebase.google.com/docs/cloud-messaging/concept-options#ttl

    new-feature 
    opened by sasabajic 1
  • A helper method for adding notification sound?

    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);
    }
    
    opened by CharlieHorton 1
  • Icon option doesn't work

    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.

    opened by otavioq 1
  • Unable to send notification

    Unable to send notification

    Hi,

    I'm currently facing the following error when I try to send out a Firebase Message and I can't seem to figure out the cause for the NotRegistered error:

    {"multicast_id":4071821441044791234,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"}]}

    Appreciate if you can assist me on this.

    opened by zhpeh 1
  • Got undefined

    Got undefined

    TEstes using you code, but sometimes it shows undefined and everything seems normal.

    ` $deviceTokens = User::whereNotNull('fcm_token')->pluck('fcm_token')->toArray();

        return Larafirebase::withTitle('Test Title')
        ->withBody('Test body')
        ->withImage('https://firebase.google.com/images/social.png')
        ->withIcon('https://seeklogo.com/images/F/firebase-logo-402F407EE0-seeklogo.com.png')
        ->withSound('default')
        ->withClickAction('https://www.google.com')
        ->withPriority('high')
        ->withAdditionalData([
            'color' => '#rrggbb',
            'badge' => 0,
        ])
        ->sendNotification($deviceTokens);`
    

    Screenshot_20220902_155030

    opened by jhoanborges 0
Releases(1.3.5)
  • 1.3.5(Jul 26, 2022)

    What's Changed

    • [1.x] Return http response instead of body by @eiabea in https://github.com/kutia-software-company/larafirebase/pull/48

    New Contributors

    • @eiabea made their first contribution in https://github.com/kutia-software-company/larafirebase/pull/48

    Full Changelog: https://github.com/kutia-software-company/larafirebase/compare/1.3.4...1.3.5

    Source code(tar.gz)
    Source code(zip)
  • 1.3.4(Jul 18, 2022)

    What's Changed

    • [Feat]: Sound for the notification by @ferasbbm in https://github.com/kutia-software-company/larafirebase/pull/45

    New Contributors

    • @ferasbbm made their first contribution in https://github.com/kutia-software-company/larafirebase/pull/45

    Full Changelog: https://github.com/kutia-software-company/larafirebase/compare/1.3.3...1.3.4

    Source code(tar.gz)
    Source code(zip)
  • 1.3.3(Mar 22, 2022)

    What's Changed

    • Laravel 9 support by @oriceon in https://github.com/kutia-software-company/larafirebase/pull/37

    New Contributors

    • @oriceon made their first contribution in https://github.com/kutia-software-company/larafirebase/pull/37

    Full Changelog: https://github.com/kutia-software-company/larafirebase/compare/1.3.2...1.3.3

    Source code(tar.gz)
    Source code(zip)
  • 1.3.2(Jan 10, 2022)

    What's Changed

    • Added additionalData property to attach arbitrary key-value pairs by @nathangaskin in https://github.com/kutia-software-company/larafirebase/pull/33

    New Contributors

    • @nathangaskin made their first contribution in https://github.com/kutia-software-company/larafirebase/pull/33

    Full Changelog: https://github.com/kutia-software-company/larafirebase/compare/1.3.1...1.3.2

    Source code(tar.gz)
    Source code(zip)
  • 1.3.1(Oct 31, 2021)

  • 1.3.0(Mar 29, 2021)

  • 1.2.0(Feb 21, 2021)

    1.2.0

    • Merged pull request from @astritzeqiri to create MIT License for package (4b222a0) 😊.
    • Added helper larafirebase() (2006d94) ❤️.
    • Added method fromRaw to build payload from scratch (7a37a67) 🚀.
    • Added Tips & Payload section to ReadMe (6840422) 👌.
    Source code(tar.gz)
    Source code(zip)
  • 1.0.7(Feb 15, 2021)

  • 1.0.6(Feb 11, 2021)

  • 1.0.5(Jan 31, 2021)

  • 1.0.1(Jan 29, 2021)

  • 1.0.0(Jan 29, 2021)

Owner
Kutia Software Company
CUSTOM SOFTWARE DEVELOPMENT AND DESIGN
Kutia Software Company
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

Laravel Notification Channels 564 Jan 3, 2023
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

gomoob 63 Sep 28, 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:

Vitaliy Zhuk 157 Dec 1, 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.

null 1.5k Jan 7, 2023
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

Davi Nunes 1.2k Sep 27, 2022
Push Notifications using Laravel

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

Webelight Solutions 26 Jul 22, 2022
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

skrepr 20 Oct 4, 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

Cédric Dugat 1.2k Jan 3, 2023
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

Mathijs Kadijk 223 Sep 28, 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

Norifumi SUNAOKA 3 Dec 14, 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

Mathijs Kadijk 223 Sep 28, 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

Cédric Dugat 1.2k Jan 3, 2023
Service that helps you to send notifications for a series of failed exceptions.

Laravel Failure Notifier This package helps you to track your exceptions and do what you want to do with them such as sending an SMS or and Email. You

Kamyar Gerami 7 Nov 26, 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

Team Nifty GmbH 25 Dec 1, 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

Ayman Alaiwah 3 May 7, 2022
: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 (

JoliCode 1.2k Dec 29, 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

Filipe Dobreira 51 Mar 26, 2019
It's Pimcore Bundle to send notifications to Google Chat, Slack or Email from admin panel inside Pimcore

Send notifications to Discord, Google Chat, Slack and more from Pimcore It's Pimcore Bundle to send notifications to Discord, Google Chat, Slack, Tele

LemonMind.com 6 Aug 31, 2022
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

Symfony 610 Jan 3, 2023