laravel-notification-channels / webpush
Laravel 的 Web 推送通知驱动程序。
Requires
- php: ^8.1
- illuminate/notifications: ^9.0|^10.0|^11.0
- illuminate/support: ^9.0|^10.0|^11.0
- minishlink/web-push: ^9.0
Requires (Dev)
- mockery/mockery: ~1.0
- orchestra/testbench: ^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.5|^10.5
README
此包使得使用 Laravel 发送 Web 推送通知变得简单。
安装
您可以通过 composer 安装此包
composer require laravel-notification-channels/webpush
首先将 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
。
注意:如果目标 Safari 或 iOS 在 2023 年之后,您还需要包含
VAPID_SUBJECT
变量,否则 Apple 将返回BadJwtToken
错误。
这些密钥必须安全存储,并且不应更改。
如果您仍然希望支持 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 (new WebPushMessage) ->title('Approved!') ->icon('/approved-icon.png') ->body('Your account was approved!') ->action('View account', 'view_account') ->options(['TTL' => 1000]); // ->data(['id' => $notification->id]) // ->badge() // ->dir() // ->image() // ->lang() // ->renotify() // ->requireInteraction() // ->tag() // ->vibrate() } }
您可以在 此处 找到可用的选项。
保存/更新订阅
要保存或更新订阅,请使用您的用户上的 updatePushSubscription($endpoint, $key = null, $token = null, $contentEncoding = null)
方法
$user = \App\User::find(1); $user->updatePushSubscription($endpoint, $key, $token, $contentEncoding);
$key
和 $token
是可选的,并用于加密您的通知。只有加密的通知才能有有效载荷。
删除订阅
要删除订阅,请使用您的用户上的 deletePushSubscription($endpoint)
方法
$user = \App\User::find(1); $user->deletePushSubscription($endpoint);
浏览器兼容性
查看 Push API 浏览器兼容性。
更新日志
请参阅 CHANGELOG 以获取有关最近更改的更多信息。
测试
$ composer test
安全
如果您发现任何安全相关的问题,请通过电子邮件 themsaid@gmail.com 反馈,而不是使用问题跟踪器。
贡献
请参阅 CONTRIBUTING 以获取详细信息。
鸣谢
许可
MIT 许可证 (MIT)。请参阅 许可文件 以获取更多信息。