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]);
}
}
The Web Push notification channel can be installed easily via Composer:
composer require xcoorp/laravel-webpush-notifications
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
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.
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
]
}
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
In order to ensure that the community is welcoming to all, please review and abide by the Code of Conduct.
Please review the security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.