kemalnw / laravel-fcm
一个用于通过 Firebase Cloud Messaging (FCM) 发送通知的 Laravel 扩展包
v1.0.5
2020-10-31 05:57 UTC
Requires
- php: ^7.2
- guzzlehttp/guzzle: ^7.2
- illuminate/notifications: ^5.5|^6.0|^7.0|^8.0
- illuminate/support: ^5.5|^6.0|^7.0|^8.0
Requires (Dev)
- orchestra/testbench: ^3.5|^4.0|^5.0
- phpunit/phpunit: ^7.5
README
一个用于通过 Firebase Cloud Messaging (FCM) 发送通知的 Laravel 扩展包
安装
在您的 composer.json
中要求 kemalnw/laravel-fcm
包并更新依赖项
composer require kemalnw/laravel-fcm
配置
您必须发布配置文件来定义您的 Firebase 服务器密钥
php artisan vendor:publish --tag="fcm"
这是在 config/fcm.php
中发布的配置文件内容
/** * Define your firebase server key */ return [ 'server_key' => env('FIREBASE_SERVER_KEY', ''), ];
用法
使用 artisan 命令创建通知
php artisan make:notification SomeNotification
更改 via
方法,使其变为
/** * Get the notification channels. * * @param mixed $notifiable * @return array|string */ public function via($notifiable) { return ['fcm']; }
向您的通知中添加 toFcm
方法,并返回 Fcm
Facade 的实例。
use Fcm; ... /** * Get the FCM representation of the notification. * * @param mixed $notifiable * @return void */ public function toFcm($notifiable) { return Fcm::notification([ 'title' => 'Hi!', 'body' => 'This is my first notification.' ]) ->timeToLive(604800); // 7 days in second }
在发送到特定设备时,通知系统将自动在您的可通知实体上查找 firebase_uid
属性。您可以通过在实体上定义 routeNotificationForFcm
方法来自定义用于发送通知的 firebase token
... class User extends Authenticatable { use Notifiable; /** * Route notifications for the FCM channel. * * @param \Illuminate\Notifications\Notification $notification * @return string */ public function routeNotificationForFcm($notification) { return $this->firebase_uid; } }
在发送到主题时,您可以在通知中的 toFcm
方法内定义
use Fcm; ... /** * Get the FCM representation of the notification. * * @param mixed $notifiable * @return void */ public function toFcm($notifiable) { return Fcm::notification([ 'title' => 'Hi!', 'body' => 'This is my first notification.' ]) ->timeToLive(604800) // 7 days in second ->toTopic('topic-name'); }