定制 laravel-notification-channels/webpush 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

laravel-notification-channels/webpush

Composer 安装命令:

composer require laravel-notification-channels/webpush

包简介

Web Push Notifications driver for Laravel.

README 文档

README

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

This package makes it easy to send web push notifications with Laravel.

Features

  • Easy integration with Laravel notifications: Seamlessly integrates with Laravel's notification system, allowing you to send web push notifications using familiar notification channels.
  • Multiple browser support: Works with all major browsers that implement the Push API, including Chrome, Firefox, Edge, and Safari.
  • VAPID support: Easily generate and manage VAPID keys for secure browser authentication.
  • Flexible message options: Customize notifications with title, body, icon, actions, TTL, and more. Supports advanced options like vibration, badge, image, and custom data payloads.
  • Subscription management: Convenient methods to save, update, and delete push subscriptions directly on your notifiable models.
  • Automatic expired subscription cleanup: When sending a push message to an expired subscription, the package detects expired endpoints (using the expired flag of Minishlink\WebPush\MessageSentReport). This package will automatically delete expired subscriptions.
  • Configurable and extendable: Publish and customize the package's config file to fit your application's needs.

Installation

You can install the package via Composer:

composer require laravel-notification-channels/webpush

First, add the NotificationChannels\WebPush\HasPushSubscriptions trait to your User model:

use NotificationChannels\WebPush\HasPushSubscriptions;

class User extends Model
{
    use HasPushSubscriptions;
}

Next, publish the migration with:

php artisan vendor:publish --provider="NotificationChannels\WebPush\WebPushServiceProvider" --tag="migrations"

Run the migrate command to create the necessary table:

php artisan migrate

You can also publish the config file with:

php artisan vendor:publish --provider="NotificationChannels\WebPush\WebPushServiceProvider" --tag="config"

Generate the VAPID keys (required for browser authentication) with:

php artisan webpush:vapid

This command will set VAPID_PUBLIC_KEY and VAPID_PRIVATE_KEY in your .env file. You need the VAPID_PUBLIC_KEY as applicationServerKey when using the Push API.

Note for Safari:

  • If targeting Safari or iOS (especially after 2023), you must include the VAPID_SUBJECT variable in your .env file. This should be a valid URL (e.g. https://example.com) or a mailto: address (e.g. mailto:admin@example.com).
  • Apple will reject requests with a BadJwtToken error if VAPID_SUBJECT is missing or invalid.
  • The domain in VAPID_SUBJECT must be a valid, existing top-level domain (TLD).

These keys must be safely stored and should not change.

Usage

Now you can use the channel in your via() method inside the notification and send a generic web push notification:

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()
    }
}

Declarative Web Push messages

Note: The specification for Declarative Web Push messages is still evolving and may change in the future. Browser support for this functionality is currently limited and may vary across platforms.

This package also supports Declarative Web Push messages, which aim to reduce the complexity of using push on the web in general and address some challenges of generic web push notifications like privacy concerns & battery life on mobile by making a client-side service worker optional while remaining fully backwards compatible:

use Illuminate\Notifications\Notification;
use NotificationChannels\WebPush\DeclarativeWebPushMessage;
use NotificationChannels\WebPush\WebPushChannel;

class AccountApproved extends Notification
{
    public function via($notifiable)
    {
        return [WebPushChannel::class];
    }

    public function toWebPush($notifiable, $notification)
    {
        return (new DeclarativeWebPushMessage)
            ->title('Approved!')
            ->icon('/approved-icon.png')
            ->body('Your account was approved!')
            ->action('View account', 'view_account', 'https://myapp.com/accounts')
            ->navigate('https://myapp.com');
            // ->data(['id' => $notification->id])
            // ->badge()
            // ->dir()
            // ->image()
            // ->lang()
            // ->mutable()
            // ->renotify()
            // ->requireInteraction()
            // ->silent()
            // ->tag()
            // ->timestamp()
            // ->vibrate()
            // ->options(['TTL' => 1000, 'contentType' => 'application/json'])
    }
}

You can find the available options here.

Save/Update Subscriptions

To save or update a subscription, use the updatePushSubscription($endpoint, $key = null, $token = null, $contentEncoding = null) method on your user:

$user = \App\User::find(1);

$user->updatePushSubscription($endpoint, $key, $token, $contentEncoding);

The $key and $token are optional and are used to encrypt your notifications. However, all major browsers require encryption when sending notifications.

When using the Push API, $key is the value of the getKey('p256dh') method, and $token is the value of the getKey('auth') method of the PushSubscription interface.

Delete Subscriptions

To delete a subscription, use the deletePushSubscription($endpoint) method on your user:

$user = \App\User::find(1);

$user->deletePushSubscription($endpoint);

Browser Compatibility

See the Push API browser compatibility.

Changelog

Please see CHANGELOG for more information about what has changed recently.

Testing

composer test

Security

If you discover any security-related issues, please email themsaid@gmail.com instead of using the issue tracker.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

laravel-notification-channels/webpush 适用场景与选型建议

laravel-notification-channels/webpush 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.4M 次下载、GitHub Stars 达 890, 最近一次更新时间为 2016 年 08 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 laravel-notification-channels/webpush 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 laravel-notification-channels/webpush 我们能提供哪些服务?
定制开发 / 二次开发

基于 laravel-notification-channels/webpush 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 5.4M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 894
  • 点击次数: 31
  • 依赖项目数: 26
  • 推荐数: 0

GitHub 信息

  • Stars: 890
  • Watchers: 22
  • Forks: 123
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-08-12