zupago / webpush
Laravel 的 Web 推送通知驱动程序。
Requires
- php: ^7.0
- illuminate/notifications: ^5.3
- illuminate/support: ^5.1
- minishlink/web-push: ^2.0
Requires (Dev)
- mockery/mockery: ~1.0
- orchestra/testbench: ^3.5
- phpunit/phpunit: ~6.0
This package is not auto-updated.
Last update: 2020-01-24 17:29:02 UTC
README
此包使您能够轻松使用 Laravel 发送 Web 推送通知。
安装
您可以通过 composer 安装此包
composer require zupago/webpush
首先,您必须安装服务提供者(对于 Laravel >= 5.5 可跳过)
// config/app.php 'providers' => [ ... NotificationChannels\webpush\webpushServiceProvider::class, ],
将 NotificationChannels\webpush\HasPushSubscriptions
特性添加到您的 User
模型
use NotificationChannels\webpush\HasPushSubscriptions; class User extends Model { use HasPushSubscriptions; }
接下来,使用以下命令发布迁移
php artisan vendor:publish --provider="NotificationChannels\webpush\webpushServiceProvider" --tag="migrations"
运行迁移命令以创建必要的表
php artisan migrate
您还可以使用以下命令发布配置文件
php artisan vendor:publish --provider="NotificationChannels\webpush\webpushServiceProvider" --tag="config"
使用以下命令生成 VAPID 密钥(浏览器身份验证所必需)
php artisan webpush:vapid
此命令将在您的 .env
文件中设置 VAPID_PUBLIC_KEY
和 VAPID_PRIVATE_KEY
。
这些密钥必须安全存储,并且不应更改。
如果您还想支持 Google Cloud Messaging,请在您的 .env
文件中设置 GCM_KEY
和 GCM_SENDER_ID
。
使用方法
现在您可以在通知中的 via()
方法内部以及发送 Web 推送通知时使用该通道
use Illuminate\Notifications\Notification; use NotificationChannels\webpush\webpushMessage; use NotificationChannels\webpush\webpushChannel; class AccountApproved extends Notification { public function via($notifiable) { return [webpushChannel::class]; } public function towebpush($notifiable, $notification) { return webpushMessage::create() // ->id($notification->id) ->title('Approved!') ->icon('/approved-icon.png') ->body('Your account was approved!') ->action('View account', 'view_account'); } }
保存/更新订阅
要保存或更新订阅,请在您的用户上使用 updatePushSubscription($endpoint, $key = null, $token = null)
方法
$user = \App\User::find(1); $user->updatePushSubscription($endpoint, $key, $token);
$key
和 $token
是可选的,用于加密您的通知。只有加密通知才能有负载。
删除订阅
要删除订阅,请在您的用户上使用 deletePushSubscription($endpoint)
方法
$user = \App\User::find(1); $user->deletePushSubscription($endpoint);
浏览器兼容性
当前 Push API 在 Chrome 和 Firefox 上工作。
变更日志
请参阅 CHANGELOG 了解最近发生了什么变化。
测试
$ composer test