laravel-notification-channels / webpush
Web Push Notifications driver for Laravel.
Installs: 2 427 462
Dependents: 14
Suggesters: 0
Security: 0
Stars: 688
Watchers: 25
Forks: 120
Open Issues: 24
Requires
- php: ^8.1
- illuminate/notifications: ^9.0|^10.0|^11.0
- illuminate/support: ^9.0|^10.0|^11.0
- minishlink/web-push: ^9.0
Requires (Dev)
- mockery/mockery: ~1.0
- orchestra/testbench: ^7.0|^8.0|^9.0
- phpunit/phpunit: ^9.5|^10.5
README
This package makes it easy to send web push notifications with Laravel.
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.
Note: if targeting Safari or iOS after 2023, you will need to include the
VAPID_SUBJECT
variable as well or Apple will return aBadJwtToken
error.
These keys must be safely stored and should not change.
If you still want support for Google Cloud Messaging, set the GCM_KEY
and GCM_SENDER_ID
in your .env
file.
Usage
Now you can use the channel in your via()
method inside the notification as well as send a 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() } }
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. Only encrypted notifications can have a payload.
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.