定制 cofa/notification_via_firebase_and_database 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cofa/notification_via_firebase_and_database

Composer 安装命令:

composer require cofa/notification_via_firebase_and_database

包简介

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

README 文档

README

CI Latest Version on Packagist Total Downloads License: MIT PHP Version

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

  • 🔥 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

Requirement Version
PHP ^8.2
Laravel ^10.0 | ^11.0 | ^12.0
kreait/firebase-php ^7.0 | ^8.0

Installation

Install via Composer:

composer require cofa/notification_via_firebase_and_database

Register the Service Provider

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

Laravel 10 — add to config/app.php:

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

Configuration

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

  1. Go to Firebase Console → 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 .gitignorenever commit credentials to version control
storage/app/firebase/

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

Column Type Description
user_id foreignId References users.id
device_token string Unique FCM device token
device_type enum android, ios, web
device_name string (nullable) Optional device label
is_active boolean Whether token is active
last_used_at timestamp (nullable) Last delivery timestamp

Usage

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)

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

<?php

namespace App\Notifications;

use Cofa\NotificationViaFirebaseAndDatabase\Contracts\FirebaseNotification;
use Cofa\NotificationViaFirebaseAndDatabase\Contracts\FirebasePayload;

class OrderShipped extends FirebaseNotification
{
    public function __construct(private array $orderData)
    {
        $payload = new FirebasePayload();
        $payload->setData([
            'notification' => [
                'title' => 'Order Shipped',
                'body'  => "Your order #{$orderData['order_id']} has been shipped!",
            ],
            'data' => [
                'order_id' => (string) $orderData['order_id'],
                'type'     => 'order_shipped',
            ],
        ]);

        $payload->setAndroidConfiguration(['priority' => 'high']);

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

        parent::__construct($payload);
    }

    public function via($notifiable): array
    {
        return ['firebase', 'database'];
    }

    // Optional: customize what gets stored in the database
    public function toDatabase($notifiable): array
    {
        return [
            'order_id' => $this->orderData['order_id'],
            'message'  => 'Your order has been shipped',
        ];
    }
}

Send it:

$user->notify(new OrderShipped(['order_id' => 12345]));

3. Firebase-Only Notification

use Cofa\NotificationViaFirebaseAndDatabase\Contracts\FirebaseNotification;
use Cofa\NotificationViaFirebaseAndDatabase\Contracts\FirebasePayload;

class PromoAlert extends FirebaseNotification
{
    public function __construct(string $message)
    {
        $payload = new FirebasePayload();
        $payload->setData([
            'notification' => ['title' => 'Special Offer', 'body' => $message],
        ]);
        parent::__construct($payload);
    }

    public function via($notifiable): array
    {
        return ['firebase'];
    }
}

$user->notify(new PromoAlert('Get 50% off today only!'));

4. Database-Only Notification

use Cofa\NotificationViaFirebaseAndDatabase\Contracts\DatabaseNotification;

$user->notify(new DatabaseNotification([
    'type'    => 'welcome',
    'message' => 'Welcome to the platform!',
]));

5. Reading Notifications

// Get all notifications
$notifications = $user->notifications;

// Get only unread
$unread = $user->unreadNotifications;

// Mark all as read
$user->unreadNotifications->markAsRead();

Advanced Usage

Platform-Specific Payload Configuration

$payload = new FirebasePayload();

$payload->setData([
    'notification' => ['title' => 'Hello', 'body' => 'World'],
    'data'         => ['deep_link' => 'app://home'],
]);

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

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

Error Handling

Always wrap notifications in a try/catch for production use:

try {
    $user->notify(new OrderShipped($orderData));
} catch (\RuntimeException $e) {
    Log::error('Notification failed', [
        'user_id' => $user->id,
        'error'   => $e->getMessage(),
    ]);
}

Common failure reasons:

  • Missing credentials — credentials file path is wrong or file doesn't exist
  • Invalid FCM token — token has expired or the app was uninstalled
  • Network error — FCM service unreachable

Tip: When an FCM token returns a NOT_REGISTERED error from Firebase, delete it from user_device_tokens to keep your token table clean.

Extending the Package

You can implement the Notification interface to create fully custom channels:

use Cofa\NotificationViaFirebaseAndDatabase\Contracts\Notification;

class SmsNotification implements Notification
{
    public function __construct(private array $data) {}

    public function sendNotification(array $targets): void
    {
        // your SMS logic here
    }
}

Architecture

src/
├── Channels/
│   ├── FirebaseChannel.php               # Sends to FCM
│   └── DatabaseChannel.php              # Stores in database
├── Contracts/
│   ├── Notification.php                 # Base notification interface
│   ├── Payload.php                      # Base payload class
│   ├── FirebasePayload.php              # Firebase-specific payload
│   ├── DatabasePayload.php              # Database-specific payload
│   ├── FirebaseNotification.php         # Firebase notification implementation
│   └── DatabaseNotification.php        # Database notification implementation
└── FirebaseNotificationServiceProvider.php

Testing

# Run the test suite
composer test

# Run with coverage report
vendor/bin/phpunit --coverage-html coverage

# Run static analysis
composer psalm

Security

  • ❌ Never commit your Firebase credentials JSON to version control
  • ✅ Add the credentials path to .gitignore
  • ✅ Use environment variables to define the credentials path
  • ✅ Validate all user input before building notification payloads
  • ✅ Regularly rotate Firebase service account keys

Contributing

Contributions, issues, and feature requests are welcome! Feel free to:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/my-feature)
  3. Commit your changes (git commit -m 'feat: add my feature')
  4. Push to the branch (git push origin feature/my-feature)
  5. Open a Pull Request

Please make sure your PR includes tests and passes composer test and composer psalm.

Changelog

v2.0.0

  • Refactored to use Laravel's native notification channel architecture
  • Added via(), toFirebase(), toDatabase() channel support
  • Improved service provider with auto-discovery for Laravel 11+
  • Removed tymon/jwt-auth conflict
  • Added tymon/jwt-auth to dev dependencies to verify compatibility

v1.1.2

  • Added conflict declaration with tymon/jwt-auth

v1.1.1

  • Added setAndroidConfiguration() and setIOSConfiguration() to payload

v1.1.0

  • Improved payload contract structure

v1.0.x

  • Initial releases

License

This package is open-source software licensed under the MIT license.

Credits

Author: Mahmoud Gamal Email: mgcofa@gmail.com GitHub: github.com/Cofa12

Support

For bugs, questions, or feature requests, please open an issue on GitHub.

cofa/notification_via_firebase_and_database 适用场景与选型建议

cofa/notification_via_firebase_and_database 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 38 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 01 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 cofa/notification_via_firebase_and_database 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cofa/notification_via_firebase_and_database 我们能提供哪些服务?
定制开发 / 二次开发

基于 cofa/notification_via_firebase_and_database 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 38
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-15