ngoahamos / 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
README
此包简化了使用 Laravel 发送 Web 推送通知的过程。
安装
您可以通过 composer 安装此包
composer require laravel-notification-channels/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,请将 GCM_KEY
和 GCM_SENDER_ID
设置在您的 .env
文件中。
使用
现在,您可以在通知中的 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 (new WebPushMessage) ->title('Approved!') ->icon('/approved-icon.png') ->body('Your account was approved!') ->action('View account', 'view_account'); // ->data(['id' => $notification->id]) // ->badge() // ->dir() // ->image() // ->lang() // ->renotify() // ->requireInteraction() // ->tag() // ->vibrate() } }
保存/更新订阅
要保存或更新订阅,请使用您的用户上的 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);
演示
有关带有 Service Worker 的完整实现,请参阅此 演示。
浏览器兼容性
请参阅 Push API 浏览器兼容性。
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG。
测试
$ composer test
安全
如果您发现任何与安全相关的问题,请通过电子邮件 themsaid@gmail.com 报告,而不是使用问题跟踪器。
贡献
有关详细信息,请参阅 CONTRIBUTING。
致谢
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件。