paxha/laravel-fcm
Composer 安装命令:
composer require paxha/laravel-fcm
包简介
This package provides (Laravel Notification) channels for sending notifications via FCM (Firebase Cloud Messaging) using HTTP v1 API.
README 文档
README
Introduction
This package provides (Laravel Notification) channels for sending notifications via FCM (Firebase Cloud Messaging) using HTTP v1 API. For more information about the Firebase Cloud Messaging HTTP v1 API, please refer to the official documentation: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages
Installation
To get started, you need to install the package via Composer:
composer require paxha/laravel-fcm
Optional: Once the package is installed, you can publish the configuration file using the following command:
php artisan vendor:publish --tag=fcm-config
This will create a new fcm.php file in your config directory, where you can configure your FCM settings. If you
don't publish it, it will create a default channel named fcm with the default configuration.
Configuration
You can configure your FCM settings in the fcm.php file. Here's an example of the default configuration:
'fcm' => [ // this fcm key is the channel name you can create multiple channels over here... 'project' => env('GOOGLE_PROJECT'), 'service_account' => env('GOOGLE_SERVICE_ACCOUNT'), ],
You can also set your FCM settings in your .env file:
Please put your service account file at config folder and set the filename with path in the .env file.
You can create a service account file from the Google Cloud Console. For more information, please refer to the official: https://cloud.google.com/iam/docs/service-accounts-create
GOOGLE_PROJECT=your-project-id GOOGLE_SERVICE_ACCOUNT=firebase-certificates/service-account.json
Usage
To send a notification, you can use the channel provided by this package. Here's an example of how to send a
notification using the fcm channel:
Before sending the notification, you need to use our HasPushToken trait in your notifiable model.
class YourModel extends Model { use \LaravelFCM\Traits\HasPushToken; // if your model has a push token column name other than `push_token` then you can define it like this public function routeNotificationForFCM() { return $this->your_custom_column; // column name where you stored the push token } }
Create a new notification using the following command:
php artisan make:notification NewMessage
<?php namespace App\Notifications; use Illuminate\Bus\Queueable;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Notifications\Notification; class NewMessage extends Notification implements ShouldQueue { use Queueable; // take params according to your requiremnt. public function __construct( public ?string $title = null, public ?string $body = null, public $data = null, ) { } public function via() { return ['fcm']; } // optional: you can use according to your requirement. public function toFCM() { return [ 'title' => $this->title, 'body' => $this->body, ]; } // optional: you can use according to your requirement. public function toAPS() { return [ // ]; } // optional: you can use according to your requirement. public function toAndroid() { return [ // ]; } // optional: you can use according to your requirement. public function toWeb() { return [ // ]; } // optional: you can use according to your requirement. public function toData() { return $this->data; } }
Post Sending Notification
After sending the notification, it will call an event, and you can listen to the event to get the response of the notification. And you can also do some other stuff after sending the notification.
php artisan make:listener PostNotificationListener
In your EventServiceProvider:
protected $listen = [ \LaravelFCM\Events\FCMNotificationSent::class => [ PostNotificationListener::class, ], ];
In your PostNotificationListener:
public function handle(object $event): void { // you can the notifiable instance $notifiable = $event->notifiable; // you can get the notification instance $notification = $event->notification; // you can get the message instance. This is the message that was sent to FCM $message = $event->message; // you can get the response of the notification $response = $event->response; // you can also do some other stuff after sending the notification }
Cleaning up unused services OPTIONAL
There are 200+ Google API services. The chances are good that you will not want them all. In order to avoid shipping
these dependencies with your code, you can run the Google\Task\Composer::cleanup task and specify the services you want
to keep in composer.json:
{
"scripts": {
"post-autoload-dump": [
.
.
.
"Google\\Task\\Composer::cleanup"
]
},
"extra": {
.
.
"google/apiclient-services": [
"FirebaseCloudMessaging"
]
}
}
This example will remove all services other than "FirebaseCloudMessaging" when composer update or a fresh composer install is run.
IMPORTANT: If you add any services back in composer.json, you will need to remove the vendor/google/apiclient-services directory explicitly for the change you made to have effect:
rm -r vendor/google/apiclient-services composer update
paxha/laravel-fcm 适用场景与选型建议
paxha/laravel-fcm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.15k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2024 年 02 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 paxha/laravel-fcm 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 paxha/laravel-fcm 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.15k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-02-27