ankurk91/fcm-notification-channel
最新稳定版本:2.2.0
Composer 安装命令:
composer require ankurk91/fcm-notification-channel
包简介
Firebase push notification channel for Laravel
README 文档
README
Send Firebase push notifications with Laravel php framework.
Highlights
- Using the latest Firebase HTTP v1 API
- Send message to a topic or condition ????
- Send message to a specific device or multiple devices (Multicast)
- Send additional RAW data with notification
- Supports multiple Firebase projects in single Laravel app:fire:
- Invalid token handling with event and listeners
- Fully tested package with automated test cases
- Powered by battle tested Firebase php SDK ????
Installation
You can install this package via composer:
composer require "ankurk91/fcm-notification-channel"
Configuration
This package relies on laravel-firebase package to interact with Firebase
services. Here is the minimal configuration you need in your .env file
# relative or full path to the Service Account JSON file FIREBASE_CREDENTIALS=firebase-credentials.json
You will need to create a service account and place the JSON file in your project root.
Additionally, you can update your .gitignore file
/firebase-credentials*.json
Usage
You can use the FCM channel in the via() method inside your Notification class:
<?php namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue; use NotificationChannels\FCM\FCMChannel; use Kreait\Firebase\Messaging\CloudMessage; class ExampleNotification extends Notification implements ShouldQueue { use Queueable; public function via($notifiable): array { return [FCMChannel::class]; } public function toFCM($notifiable): CloudMessage { return CloudMessage::new() ->withDefaultSounds() ->withNotification([ 'title' => 'Order shipped', 'body' => 'Your order for laptop is shipped.', ]) ->withData([ 'orderId' => '#123' ]); } }
Prepare your Notifiable model:
<?php namespace App\Models; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /** * Assuming that you have a database table which stores device tokens. */ public function deviceTokens(): HasMany { return $this->hasMany(DeviceToken::class); } public function routeNotificationForFCM($notification): string|array|null { return $this->deviceTokens->pluck('token')->toArray(); } /** * Optional method to determine which message target to use * We will use TOKEN type when not specified * @see \Kreait\Firebase\Messaging\MessageTarget::TYPES */ public function routeNotificationForFCMTargetType($notification): ?string { return \Kreait\Firebase\Messaging\MessageTarget::TOKEN; } /** * Optional method to determine which Firebase project to use * We will use default project when not specified */ public function routeNotificationForFCMProject($notification): ?string { return config('firebase.default'); } }
Send to a topic or condition
This package is not limited to sending notification to tokens.
You can use Laravel's on-demand notifications to send push notification to a topic or condition or multiple tokens.
<?php use Illuminate\Support\Facades\Notification; use Kreait\Firebase\Messaging\MessageTarget; use App\Notification\ExampleNotification; Notification::route('FCM', 'topicA') ->route('FCMTargetType', MessageTarget::TOPIC) ->notify(new ExampleNotification()); Notification::route('FCM', "'TopicA' in topics") ->route('FCMTargetType', MessageTarget::CONDITION) ->notify(new ExampleNotification()); Notification::route('FCM', ['token_1', 'token_2']) ->route('FCMTargetType', MessageTarget::TOKEN) ->notify(new ExampleNotification());
Events
You can consume Laravel's inbuilt notification events
<?php namespace App\Providers; use Illuminate\Notifications\Events; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; class EventServiceProvider extends ServiceProvider { protected $listen = [ Events\NotificationSent::class => [ //\App\Listeners\FCMNotificationSent::class, ], Events\NotificationFailed::class => [ \App\Listeners\FCMNotificationFailed::class, ], ]; }
Here is the example of the failed event listener class
<?php namespace App\Listeners; use App\Models\User; use Illuminate\Support\Arr; use NotificationChannels\FCM\FCMChannel; use Illuminate\Contracts\Queue\ShouldQueue; use Kreait\Laravel\Firebase\Facades\Firebase; use Illuminate\Notifications\Events\NotificationFailed; class FCMNotificationFailed implements ShouldQueue { public function handle(NotificationFailed $event) { if ($event->channel !== FCMChannel::class) { return; } /** @var User $user */ $user = $event->notifiable; $invalidTokens = $this->findInvalidTokens($user); if (count($invalidTokens)) { $user->deviceTokens()->whereIn('token', $invalidTokens)->delete(); } } protected function findInvalidTokens(User $user): array { $tokens = Arr::wrap($user->routeNotificationFor('FCM')); if (! count($tokens)) { return []; } $project = $user->routeNotificationFor('FCMProject'); $response = Firebase::project($project)->messaging()->validateRegistrationTokens($tokens); return array_unique(array_merge($response['invalid'], $response['unknown'])); } }
Read more about validating device tokens here
Then; you may want to ignore this exception in your app/Exceptions/Handler.php
protected $dontReport = [ \NotificationChannels\FCM\Exception\InvalidRecipientException::class, ];
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
composer test
Security
If you discover any security issue, please email pro.ankurk1[at]gmail[dot]com instead of using the issue tracker.
Attribution
The package is based on this rejected PR
License
This package is licensed under MIT License.
ankurk91/fcm-notification-channel 适用场景与选型建议
ankurk91/fcm-notification-channel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 41.5k 次下载、GitHub Stars 达 72, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「notification」 「laravel」 「firebase」 「FCM」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ankurk91/fcm-notification-channel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ankurk91/fcm-notification-channel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ankurk91/fcm-notification-channel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This package makes it easy to send notifications via AfricasTalking with Laravel
simple api library.
Apple Push Notification & Feedback Provider
Throttle notifications on a per-channel basis
Yii2 Extension for sending push notification with both Firebase Cloud Messaging (FCM) HTTP Server Protocols (APIs).
Create and authenticate users with Firebase Auth providers, and let Laravel Passport handle the rest!
统计信息
- 总下载量: 41.5k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 72
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 未知