xcoorp / laravel-webpush-notifications
Web Push Notifications channel for Laravel.
Requires
- php: ^8.3
- illuminate/config: ^11.0
- illuminate/container: ^11.0
- illuminate/contracts: ^11.0
- illuminate/notifications: ^11
- illuminate/support: ^11
- minishlink/web-push: ^9
Requires (Dev)
- laravel/pint: ^v1
- pestphp/pest: ^v2
This package is auto-updated.
Last update: 2024-12-04 17:51:14 UTC
README
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 thetitle
of the messageaction(string $title, string $action, ?string $icon = null))
Set theaction
of the messagebadge(string $badge)
Set the url to thebadge
image of the messagebody(string $body)
Set thebody
of the messagedir(string $dir)
Set the text-directiondir
of the messageicon(string $icon)
Set the url to theicon
of the messageimage(string $image)
Set the url to theimage
of the messagelang(string $lang)
Set the Languagelang
of the messagerenotify(bool $renotify)
specifies whether the user should be notified after a new notification replaces an old onerequireInteraction(bool $requireInteraction)
specifies whether the notification requires interactiontag(string $value)
Set thetag
of the messagevibrate(array $vibrate)
Set the vibration patternvibrate
of the messagedata(mixed $value)
Set thedata
of the messageoptions(array $options)
Set theoptions
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.