joydeep-bhowmik / laravel-push-notification
一个用于将 Firebase Cloud Messaging (FCM) 集成到 Laravel 中以处理推送通知的 Laravel 包。
v1.0.0
2024-09-21 10:56 UTC
Requires
- kreait/firebase-php: ^6.0
- laravel/framework: ^11.9
- livewire/volt: ^1.0
Requires (Dev)
- orchestra/testbench: ^9.4
- pestphp/pest: ^3.1
- pestphp/pest-plugin-laravel: ^3.0
README
一个用于将 Firebase Cloud Messaging (FCM) 集成到 Laravel 中以无缝处理推送通知的 Laravel 包。
目录
安装
要安装此包,请使用 Composer
composer require joydeep-bhowmik/laravel-push-notification
并在您的 bootstrap/providers
中添加以下内容
<?php return [ JoydeepBhowmik\LaravelPushNotification\Providers\FcmServiceProvider::class ];
发布资源
安装后,使用以下命令发布包资源
php artisan vendor:publish --tag=fcm-all php artisan vendor:publish --tag=fcm-tokens-model
此命令将发布以下资源
- 配置文件:
config/fcm.php
- Firebase 服务工作者:
public/firebase-messaging-sw.js
- 主 JavaScript 文件:
public/js/fcm.js
- Firebase Auth 脚本:
public/js/firebase-auth.js
- 用户设备模型:
app/Models/UserDevice.php
- 用户设备迁移:
database/migrations/
(带时间戳) - 推送通知开关 Blade 组件:
resources/views/components/push-notification-switch.blade.php
配置
发布后,在 config/fcm.php
中配置 FCM 设置。请确保设置正确的 Firebase 凭据和其他选项(如有必要)。
请确保在您的 .env
文件中设置 FIREBASE_CREDENTIALS
。
并在 public/firebase-messaging-sw.js
、resources/js/fcm.js
和 storage/framework/app/firebase-auth.js
中替换 API 密钥。
迁移
运行迁移以创建必要的数据库表
php artisan migrate
用法
发送通知
要发送通知,创建一个使用 FcmChannel
的通知类。
示例通知类
以下是如何创建使用 FcmChannel
的通知类的示例
namespace App\Notifications; use Illuminate\Notifications\Notification; use Kreait\Firebase\Messaging\WebPushConfig; use Kreait\Firebase\Messaging\ApnsConfig; use Kreait\Firebase\Messaging\AndroidConfig; class YourNotification extends Notification { public function via($notifiable) { return [FcmChannel::class]; } public function toFcm($notifiable) { return [ // 'topic'=>'topic-name', if you want to send message in a topic 'notification' => [ 'title' => 'Notification Title', 'body' => 'Notification body text.', 'icon' => 'https://your-server.example/icon.png', ], 'webpush' => [ 'notification' => [ 'title' => 'Notification Title', 'body' => 'Notification body text.', 'icon' => 'https://your-server.example/icon.png', ], ], 'android' => [ 'notification' => [ 'title' => 'Android Notification Title', 'body' => 'Android notification body text.', 'icon' => 'android-icon', 'color' => '#f45342', ], ], 'apns' => [ 'payload' => [ 'aps' => [ 'alert' => [ 'title' => 'APNs Notification Title', 'body' => 'APNs notification body text.', ], 'badge' => 42, 'sound' => 'default', ], ], ], ]; } }
前端设置
请确保在您的应用程序中包含必要的 JavaScript 文件以处理推送通知。您可以在主 JavaScript 文件中添加以下内容
// resources/js/app.js import "./fcm";
请确保在 fcm.js
中的 firebaseConfig
和服务工作者中替换占位符
const firebaseConfig = { apiKey: "YOUR_API_KEY_HERE", authDomain: "YOUR_AUTH_DOMAIN_HERE", projectId: "YOUR_PROJECT_ID_HERE", storageBucket: "YOUR_STORAGE_BUCKET_HERE", messagingSenderId: "YOUR_MESSAGING_SENDER_ID_HERE", appId: "YOUR_APP_ID_HERE", measurementId: "YOUR_MEASUREMENT_ID_HERE", };
推送通知开关组件
您可以使用 push-notification-switch
Blade 组件在视图中允许用户启用或禁用推送通知。
<x-push-notification-switch />
许可协议
此包根据 MIT 许可协议授权。有关更多信息,请参阅 LICENSE 文件。