kz370/laravel-firebase-notifications 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

kz370/laravel-firebase-notifications

Composer 安装命令:

composer require kz370/laravel-firebase-notifications

包简介

A Laravel package for sending Firebase Cloud Messaging (FCM) notifications using service accounts.

README 文档

README

A simple and flexible Laravel package for sending Firebase Cloud Messaging (FCM) notifications using a Firebase Service Account. Supports both direct (instant) and queued (asynchronous) notifications — including topic-based broadcasts.

🧩 Features

  • ✅ Send notifications to individual devices or specific topics
  • ✅ Supports queued notifications for scalable delivery
  • ✅ Uses Firebase Admin REST API v1 (no legacy FCM keys)
  • ✅ Fully configurable via .env
  • ✅ Compatible with Laravel 9, 10, and 11

📦 Installation

Install via Composer:

composer require kz370/laravel-firebase-notifications

⚙️ Configuration

Publish the configuration file:

php artisan vendor:publish --provider="Kz370\FirebaseNotifications\FirebaseNotificationServiceProvider" --tag=config

This creates config/firebase-notifications.php.

.env Setup

Add your Firebase credentials and project ID:

FIREBASE_SERVICE_ACCOUNT=storage/app/firebase-service-account.json
FIREBASE_PROJECT_ID=your-firebase-project-id

💡 Tip: Download your Firebase service account key from Project Settings → Service Accounts → Generate New Private Key

🚀 Usage Examples

Send Notification to a Specific Device

use FirebaseNotification;

$deviceToken = 'your-device-token-here';

FirebaseNotification::sendNotification(
    $deviceToken,
    'Hello there!',
    'Welcome to our platform 🎉',
    'https://example.com/welcome-banner.png',
    ['type' => 'welcome', 'user_id' => 123]
);

Method: sendNotification()

This method sends a push notification directly to a specific device using its FCM device token.

sendNotification(
    string $deviceToken,
    string $title,
    string $body,
    ?string $imageUrl = null,
    array $data = []
): array

Parameters:

Parameter Type Required Description
$deviceToken string The Firebase device token that identifies a specific user's device. You typically get this from your mobile app's Firebase SDK.
$title string The title text of the notification, usually displayed in bold (e.g., "New Message").
$body string The main message content or body text of the notification.
$imageUrl string|null (Optional) URL of an image to display in the notification if supported by the client app.
$data array (Optional) A custom key-value data payload sent alongside the notification. Useful for deep links or background processing.

Send Notification to a Topic

You can send notifications to any topic your users have subscribed to. By default, the topic is "all" — but you can specify your own.

FirebaseNotification::sendNotificationToTopic(
    'System Update',
    'We're upgrading our system tonight.',
    'https://example.com/update-banner.png',
    ['maintenance_mode' => true],
    'news'
);

This will send the notification to all users subscribed to the topic news.

Method: sendNotificationToTopic()

sendNotificationToTopic(
    string $title,
    string $body,
    ?string $imageUrl = null,
    array $data = [],
    string $topic = 'all'
): array

Parameters:

Parameter Type Required Description
$title string The notification title shown in the header (e.g., "System Update")
$body string The main notification body text (e.g., "We're upgrading our system tonight.")
$imageUrl string|null Optional image URL to display in the notification (if supported)
$data array Custom key-value payload for background handling or deep linking
$topic string Target topic (default: "all")

⏳ Using Queues

You can queue notifications for background processing using Laravel's queue system.

Queue Single Device Notification

use Kz370\FirebaseNotifications\Jobs\FirebaseNotificationJob;

FirebaseNotificationJob::dispatch(
    $deviceToken,
    'Queued Message',
    'This notification is sent via the queue!'
);

Queue Topic Notification

FirebaseNotificationJob::dispatch(
    null,
    'Breaking News',
    'Something big just happened!',
    null,
    ['category' => 'news'],
    true // Indicates it's for a topic
);

Delay Notification

FirebaseNotificationJob::dispatch(
    $deviceToken,
    'Reminder',
    'Your session starts soon.'
)->delay(now()->addMinutes(10));

Make sure your queue worker is running:

php artisan queue:work

⚡ Configuration Overview

config/firebase-notifications.php

return [
    'service_account_path' => env('FIREBASE_SERVICE_ACCOUNT', storage_path('app/firebase-service-account.json')),
    'project_id' => env('FIREBASE_PROJECT_ID', 'your-project-id'),
];

🧰 Troubleshooting

invalid_grant or unauthorized_client

Check that your Firebase service account JSON file is valid and the .env path is correct.

Notification not delivered

  • The device token must be valid and active
  • Ensure the app is subscribed to the topic you're sending to
  • The Firebase Cloud Messaging API must be enabled in Google Cloud Console

Queues not processing

Run your queue worker:

php artisan queue:work

🪪 License

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

✨ Author

KZ370
💻 Laravel Developer

kz370/laravel-firebase-notifications 适用场景与选型建议

kz370/laravel-firebase-notifications 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 kz370/laravel-firebase-notifications 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-10