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-notificationsAfter 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 migrateAfter 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 urlYou can generate the vapid keys using the following command:
php artisan webpush-notifications:vapidThe 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 thetitleof the messageaction(string $title, string $action, ?string $icon = null))Set theactionof the messagebadge(string $badge)Set the url to thebadgeimage of the messagebody(string $body)Set thebodyof the messagedir(string $dir)Set the text-directiondirof the messageicon(string $icon)Set the url to theiconof the messageimage(string $image)Set the url to theimageof the messagelang(string $lang)Set the Languagelangof 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 thetagof the messagevibrate(array $vibrate)Set the vibration patternvibrateof the messagedata(mixed $value)Set thedataof the messageoptions(array $options)Set theoptionsof 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.