mtgofa / fcm-laravel-notification
用于将 Laravel 的通知系统连接到 Google Firebase Cloud Messaging 的驱动程序
1.1
2020-11-17 22:19 UTC
Requires
- laravel/framework: ^6.0||^7.0||^8.0
This package is auto-updated.
Last update: 2024-09-18 06:44:40 UTC
README
此库允许通过 Laravel 的通知发送 Firebase Cloud Messaging 的推送通知。
可以发送通知到网站、Android 和 IOS,而无需任何其他集成。
安装
从您的终端运行以下命令
composer require mtgofa/fcm-laravel-notification
或者在您的 composer.json 文件的要求部分添加以下内容
"mtgofa/fcm-laravel-notification"
然后运行 composer update
安装完成后,您需要注册服务提供者。打开 config/app.php 文件,并在 providers 键下添加以下内容。
'providers' => [ ... MTGofa\FCM\NotificationServiceProvider::class ...
使用方法
首先,使用 artisan 命令创建您的通知类:php artisan make:notification MyNotification
实现您的通知
<?php namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; use MTGofa\FCM\Messages\FirebaseMessage; class MyNotification extends Notification { use Queueable; public function via($notifiable) { return ['fcm']; } public function toFcm($notifiable) { return (new FirebaseMessage())->setContent('Test Notification', 'This is a Test'); } }
在您的可通知对象上实现
use Illuminate\Database\Eloquent\Model; use Illuminate\Notifications\Notifiable; class User extends Model { use Notifiable; public function routeNotificationForFcm() { //return a device token, either from the model or from some other place. return $this->device_token; } }
打开 config/broadcasting.php 文件,并将 Firebase API 密钥添加到 connections 部分
... 'fcm' => [ 'key' => env('FCM_API_KEY','YOUR_API_KEY') ] ...
触发通知
$User = User::find(1); //Send Notification $User->notify(new MyNotification());
参考资料
有关更多信息,请参阅官方文档:https://laravel.net.cn/docs/5.3/notifications