PHPackages                             cofa/notification\_via\_firebase\_and\_database - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. cofa/notification\_via\_firebase\_and\_database

ActiveLibrary[Mail &amp; Notifications](/categories/mail)

cofa/notification\_via\_firebase\_and\_database
===============================================

This is a package for sending notificatinon via firebase and database with good code structure

v2.0.0(1mo ago)133MITPHPPHP ^8.2CI passing

Since Jan 15Pushed 1mo agoCompare

[ Source](https://github.com/Cofa12/Notification_via_firebase_and_database)[ Packagist](https://packagist.org/packages/cofa/notification_via_firebase_and_database)[ RSS](/packages/cofa-notification-via-firebase-and-database/feed)WikiDiscussions main Synced 3w ago

READMEChangelog (1)Dependencies (15)Versions (9)Used By (0)

Firebase &amp; Database Notifications for Laravel
=================================================

[](#firebase--database-notifications-for-laravel)

[![CI](https://github.com/Cofa12/Notification_via_firebase_and_database/actions/workflows/testing.yml/badge.svg)](https://github.com/Cofa12/Notification_via_firebase_and_database/actions/workflows/testing.yml/badge.svg)[![Latest Version on Packagist](https://camo.githubusercontent.com/c9987ff9187380b70c468c38166d71cca10d5e0a37c1d37e091bbdd56d6d092d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f66612f6e6f74696669636174696f6e5f7669615f66697265626173655f616e645f64617461626173652e737667)](https://packagist.org/packages/cofa/notification_via_firebase_and_database)[![Total Downloads](https://camo.githubusercontent.com/cda8bb36f1c62dd91a3584d908494748c5d12e955f091ab4ba6e1683d58e6acb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f66612f6e6f74696669636174696f6e5f7669615f66697265626173655f616e645f64617461626173652e737667)](https://packagist.org/packages/cofa/notification_via_firebase_and_database)[![License: MIT](https://camo.githubusercontent.com/784362b26e4b3546254f1893e778ba64616e362bd6ac791991d2c9e880a3a64e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d677265656e2e737667)](https://opensource.org/licenses/MIT)[![PHP Version](https://camo.githubusercontent.com/c9f64f714c636ba27a3bba6dfd52f98426832db1262747efa54b212d16943651/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382e322d626c7565)](https://www.php.net/)

A Laravel package for sending push notifications via **Firebase Cloud Messaging (FCM)** and storing them in the **database** — using Laravel's native notification pipeline with a clean, channel-based architecture.

---

Features
--------

[](#features)

- 🔥 **Firebase Channel** — Send push notifications via FCM using Laravel's standard `notify()` pipeline
- 💾 **Database Channel** — Store notifications using Laravel's built-in notification system
- 🏗️ **Channel-Based Architecture** — Fully integrated with `via()`, `toFirebase()`, and `toDatabase()`
- 📱 **Platform-Specific Configs** — Android and iOS payload configuration support
- 🔑 **Device Token Management** — Built-in `user_device_tokens` table with active/inactive tracking
- ✅ **Fully Tested** — Test suite using Orchestra Testbench and Mockery
- 🔍 **Static Analysis** — Psalm integration for type safety

---

Requirements
------------

[](#requirements)

RequirementVersionPHP`^8.2`Laravel`^10.0 | ^11.0 | ^12.0`kreait/firebase-php`^7.0 | ^8.0`---

Installation
------------

[](#installation)

Install via Composer:

```
composer require cofa/notification_via_firebase_and_database
```

### Register the Service Provider

[](#register-the-service-provider)

**Laravel 11+** — auto-discovered, no action needed.

**Laravel 10** — add to `config/app.php`:

```
'providers' => [
    Cofa\NotificationViaFirebaseAndDatabase\FirebaseNotificationServiceProvider::class,
],
```

---

Configuration
-------------

[](#configuration)

### 1. Publish the Config File

[](#1-publish-the-config-file)

```
php artisan vendor:publish --tag=firebase-notification-config
```

This creates `config/firebase-notification.php`:

```
return [
    'firebase' => [
        'credentials' => storage_path('app/firebase/credentials.json'),
    ],
];
```

### 2. Add Your Firebase Credentials

[](#2-add-your-firebase-credentials)

1. Go to [Firebase Console](https://console.firebase.google.com) → Project Settings → Service Accounts
2. Click **Generate new private key** and download the JSON file
3. Place it in your project (recommended: `storage/app/firebase/credentials.json`)
4. Add the path to `.gitignore` — **never commit credentials to version control**

```
storage/app/firebase/
```

### 3. Set Up the Database

[](#3-set-up-the-database)

```
php artisan firebase-notification:install
php artisan migrate
```

This creates two tables:

- `notifications` — Laravel's standard notifications table
- `user_device_tokens` — Stores FCM tokens per user and device

#### `user_device_tokens` Schema

[](#user_device_tokens-schema)

ColumnTypeDescription`user_id`foreignIdReferences `users.id``device_token`stringUnique FCM device token`device_type`enum`android`, `ios`, `web``device_name`string (nullable)Optional device label`is_active`booleanWhether token is active`last_used_at`timestamp (nullable)Last delivery timestamp---

Usage
-----

[](#usage)

### 1. Prepare Your User Model

[](#1-prepare-your-user-model)

Add the `Notifiable` trait and implement `routeNotificationForFirebase`:

```
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    public function routeNotificationForFirebase(): array
    {
        return $this->deviceTokens()
            ->where('is_active', true)
            ->pluck('device_token')
            ->toArray();
    }
}
```

---

### 2. Firebase + Database Notification (Combined)

[](#2-firebase--database-notification-combined)

Extend `FirebaseNotification` to send both push and database notifications at once:

```
