zareshahi/webpush

Laravel的Web推送通知驱动程序。

v1.0.0 2023-04-30 18:05 UTC

This package is not auto-updated.

Last update: 2024-09-30 23:31:17 UTC


README

Latest Version on Packagist Build Status Quality Score Code Coverage Total Downloads

此包使您能够通过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_KEYVAPID_PRIVATE_KEY

这些密钥必须安全存储,并且不应更改。

如果您仍然需要Google Cloud Messaging的支持,请在您的.env文件中设置GCM_KEYGCM_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)。请参阅许可文件以获取更多信息。