xcoorp/laravel-webpush-notifications

Web Push Notifications channel for Laravel.

v1.0.1 2024-12-04 17:50 UTC

This package is auto-updated.

Last update: 2024-12-04 17:51:14 UTC


README

Software License Laravel Version Requirements

WebPush - Laravel Notification Channel

This package makes it easy to send notifications using Web Push API

class InvoicePaidNotification extends Notification
{
    // Trigger a specific notification event
    public function toWebPush($notifiable)
    {
        return (new WebPushMessage)
                ->title('Approved!')
                ->body('Your account was approved!')
                ->action('View account', 'view_account')
                ->options(['TTL' => 1000]);
    }
}

Contents

Installation

The Web Push notification channel can be installed easily via Composer:

composer require xcoorp/laravel-webpush-notifications

Publishing Configuration and Migrations

After installing the package, you can publish the configuration file and migrations by running the following command:

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

After publishing the migrations you should migrate your database:

php artisan migrate

After publishing the configuration file, you should configure the vapid keys in your .env file:

VAPID_PUBLIC_KEY=your-public-key
VAPID_PRIVATE_KEY=your-private-key
VAPID_SUBJECT=your-email or url

You can generate the vapid keys using the following command:

php artisan webpush-notifications:vapid

Configuration

The config/webpush-notifications.php configuration file allows you to configure the default Web Push options for your application.

You can define custom Models to use, and the vapid keys to use for the Web Push API.

If you define a custom UsereWebPushSubscription model you need to make sure it extends the UsereWebPushSubscription model shipped with this package.

Usage

In order to send a notification via the WebPush channel, you'll need to specify the channel in the via() method of your notification:

use NotificationChannels\WebPush\WebPushChannel;

public function via($notifiable)
{
    return [
        WebPushChannel::class
    ]
}

API Overview

WebPush Message

Namespace: NotificationChannels\WebPush\WebPushMessage

The WebPushMessage class encompasses an entire message that will be sent to the Web Push API.

  • title(string $title) Set the title of the message
  • action(string $title, string $action, ?string $icon = null)) Set the action of the message
  • badge(string $badge) Set the url to the badge image of the message
  • body(string $body) Set the body of the message
  • dir(string $dir) Set the text-direction dir of the message
  • icon(string $icon) Set the url to the icon of the message
  • image(string $image) Set the url to the image of the message
  • lang(string $lang) Set the Language lang of the message
  • renotify(bool $renotify) specifies whether the user should be notified after a new notification replaces an old one
  • requireInteraction(bool $requireInteraction) specifies whether the notification requires interaction
  • tag(string $value) Set the tag of the message
  • vibrate(array $vibrate) Set the vibration pattern vibrate of the message
  • data(mixed $value) Set the data of the message
  • options(array $options) Set the options of the message (See here for more information)
  • toArray() Returns the data that will be sent to the WebPush API as an array

Code of Conduct

In order to ensure that the community is welcoming to all, please review and abide by the Code of Conduct.

Security Vulnerabilities

Please review the security policy on how to report security vulnerabilities.

License

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