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

v1.1.2(3mo ago)112↓100%MITPHPPHP ^8.2CI passing

Since Jan 15Pushed 3mo 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 1mo ago

READMEChangelog (1)Dependencies (6)Versions (8)Used By (0)

Firebase and Database Notification Package
==========================================

[](#firebase-and-database-notification-package)

A Laravel package for sending notifications via Firebase Cloud Messaging (FCM) and Laravel's database notification system with a clean, structured code architecture.

Features
--------

[](#features)

- 🔥 Send push notifications via Firebase Cloud Messaging
- 💾 Store notifications in database using Laravel's notification system
- 🎯 Send to single or multiple targets
- 🏗️ Clean, extensible architecture with contracts and interfaces
- ✅ Fully tested with PHPUnit

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

[](#requirements)

- PHP ^8.2
- Laravel ^10.0|^11.0|^12.0
- Firebase Admin SDK credentials

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

[](#installation)

Install the package via Composer:

```
composer require cofa/notification_via_firebase_and_database
```

### Register Service Provider

[](#register-service-provider)

If you're using Laravel 11+, the service provider will be auto-discovered. For older versions, add to `config/app.php`:

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

### Publish Configuration

[](#publish-configuration)

Publish the configuration file:

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

This will create `config/firebase-notification.php`.

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

[](#configuration)

### Firebase Setup

[](#firebase-setup)

1. Download your Firebase service account credentials JSON file from the Firebase Console
2. Place it in your Laravel project (e.g., `storage/app/firebase/credentials.json`)
3. Update `config/firebase-notification.php`:

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

### Database Setup

[](#database-setup)

#### Install Package Tables

[](#install-package-tables)

Run the installation command to set up the required database tables:

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

This command will automatically:

1. Run `php artisan notifications:table` to create the Laravel notifications table
2. Publish the `user_device_tokens` table migration

Then run the migrations:

```
php artisan migrate
```

The installation creates two tables:

- `user_device_tokens` - Stores FCM device tokens for users
- `notifications` - Laravel's default notifications table (if not already exists)

#### User Device Tokens Table Structure

[](#user-device-tokens-table-structure)

The `user_device_tokens` table includes:

- `user_id` - Foreign key to users table
- `device_token` - Unique FCM device token
- `device_type` - Device platform (android, ios, web)
- `device_name` - Optional device name
- `is_active` - Token active status
- `last_used_at` - Last time token was used

Usage
-----

[](#usage)

### 1. Firebase Notifications

[](#1-firebase-notifications)

#### Step 1: Create the Firebase Payload

[](#step-1-create-the-firebase-payload)

```
use Cofa\NotificationViaFirebaseAndDatabase\Contracts\FirebasePayload;

$payload = new FirebasePayload();

// Set Notification Data
$payload->setData([
    'notification' => [
        'title' => 'Order Shipped',
        'body' => 'Your order #12345 has been shipped!'
    ],
    'data' => [
        'order_id' => '12345',
        'tracking_number' => 'TRK123456789',
        'type' => 'order_shipped'
    ]
]);

// Set Android Configuration
$payload->setAndroidConfiguration([
    'priority' => 'high',
    'notification' => [
        'sound' => 'default',
        'color' => '#ff0000'
    ]
]);

// Set iOS Configuration
$payload->setIOSConfiguration([
    'headers' => [
        'apns-priority' => '10'
    ],
    'payload' => [
        'aps' => [
            'sound' => 'default',
            'badge' => 1
        ]
    ]
]);
```

#### Step 2: Create a Custom Firebase Notification Class

[](#step-2-create-a-custom-firebase-notification-class)

```
