paxha / laravel-fcm
此包提供了通过HTTP v1 API使用FCM(Firebase Cloud Messaging)发送通知的(Laravel Notification)通道。
Requires
- php: ^7.0|^8.0
- ext-json: *
- google/apiclient: ^2.15
Requires (Dev)
- orchestra/testbench: ^8.21
README
简介
此包提供了通过FCM(Firebase Cloud Messaging)发送通知的channels
,使用HTTP v1 API
。有关Firebase Cloud Messaging HTTP v1 API的更多信息,请参阅官方文档:https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages
安装
要开始使用,您需要通过Composer安装此包
composer require paxha/laravel-fcm
可选:安装包后,您可以使用以下命令发布配置文件
php artisan vendor:publish --tag=fcm-config
这将创建一个新文件 fcm.php
在您的 config
目录中,您可以在其中配置您的FCM设置。如果您不发布它,它将创建一个名为 fcm
的默认通道,并带有默认配置。
配置
您可以在 fcm.php
文件中配置您的FCM设置。以下是一个默认配置的示例
'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'), ],
您也可以在您的 .env
文件中设置您的FCM设置
请将服务账户文件放在 config
文件夹中,并在 .env
文件中设置带有路径的文件名。
您可以从Google Cloud Console创建服务账户文件。有关更多信息,请参阅官方文档:https://cloud.google.com/iam/docs/service-accounts-create
GOOGLE_PROJECT=your-project-id GOOGLE_SERVICE_ACCOUNT=firebase-certificates/service-account.json
使用方法
要发送通知,您可以使用此包提供的通道。以下是如何使用 fcm
通道发送通知的示例
在发送通知之前,您需要在您的可通知模型中使用我们的 HasPushToken
trait。
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 } }
使用以下命令创建一个新的通知
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; } }
发送通知后
发送通知后,它将调用一个事件,您可以监听此事件以获取通知的响应。您也可以在发送通知后进行其他操作。
php artisan make:listener PostNotificationListener
在您的 EventServiceProvider
protected $listen = [ \LaravelFCM\Events\FCMNotificationSent::class => [ PostNotificationListener::class, ], ];
在您的 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 }
清理未使用的服务(可选)
有200多个Google API服务。您可能不会想要全部使用。为了避免将这些依赖项与您的代码一起分发,您可以运行 Google\Task\Composer::cleanup
任务,并在 composer.json
中指定您想要保留的服务。
{ "scripts": { "post-autoload-dump": [ . . . "Google\\Task\\Composer::cleanup" ] }, "extra": { . . "google/apiclient-services": [ "FirebaseCloudMessaging" ] } }
此示例将在运行composer update或全新composer install时移除除 "FirebaseCloudMessaging" 以外的所有服务。
重要:如果您在 composer.json
中添加任何服务,您需要明确删除 vendor/google/apiclient-services
目录,以便您所做的更改生效。
rm -r vendor/google/apiclient-services composer update