okaufmann / laravel-webpush-channel
Laravel的Web推送通知驱动程序。
Requires
- php: ^8.0
- illuminate/notifications: ^8.0|^9.0|^10.0
- illuminate/support: ^8.0|^9.0|^10.0
- minishlink/web-push: ^8.0
Requires (Dev)
- mockery/mockery: ~1.0
- orchestra/testbench: ^6.0|^7.0|^8.0
- phpunit/phpunit: ^9.0
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。
这些密钥必须安全存储,并且不应更改。
如果您仍然需要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)。请参阅许可证文件以获取更多信息。