Web Push Notifications driver for Laravel.
laravel-notification-channels/webpush is a Laravel package for web push notifications driver for laravel..
It currently has 890 GitHub stars and 5.376.804 downloads on Packagist (latest version 11.0.0).
Install it with composer require laravel-notification-channels/webpush.
Discover more Laravel packages by laravel-notification-channels
or browse all Laravel packages to compare alternatives.
Last updated
This package makes it easy to send web push notifications with Laravel.
expired flag of Minishlink\WebPush\MessageSentReport). This package will automatically delete expired subscriptions.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. You need the VAPID_PUBLIC_KEY as applicationServerKey when using the Push API.
Note for Safari:
- If targeting Safari or iOS (especially after 2023), you must include the
VAPID_SUBJECTvariable in your.envfile. This should be a valid URL (e.g.https://example.com) or amailto:address (e.g.mailto:[email protected]).- Apple will reject requests with a
BadJwtTokenerror ifVAPID_SUBJECTis missing or invalid.- The domain in
VAPID_SUBJECTmust be a valid, existing top-level domain (TLD).
These keys must be safely stored and should not change.
Now you can use the channel in your via() method inside the notification and send a generic 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()
}
}
Note: The specification for Declarative Web Push messages is still evolving and may change in the future. Browser support for this functionality is currently limited and may vary across platforms.
This package also supports Declarative Web Push messages, which aim to reduce the complexity of using push on the web in general and address some challenges of generic web push notifications like privacy concerns & battery life on mobile by making a client-side service worker optional while remaining fully backwards compatible:
use Illuminate\Notifications\Notification;
use NotificationChannels\WebPush\DeclarativeWebPushMessage;
use NotificationChannels\WebPush\WebPushChannel;
class AccountApproved extends Notification
{
public function via($notifiable)
{
return [WebPushChannel::class];
}
public function toWebPush($notifiable, $notification)
{
return (new DeclarativeWebPushMessage)
->title('Approved!')
->icon('/approved-icon.png')
->body('Your account was approved!')
->action('View account', 'view_account', 'https://myapp.com/accounts')
->navigate('https://myapp.com');
// ->data(['id' => $notification->id])
// ->badge()
// ->dir()
// ->image()
// ->lang()
// ->mutable()
// ->renotify()
// ->requireInteraction()
// ->silent()
// ->tag()
// ->timestamp()
// ->vibrate()
// ->options(['TTL' => 1000, 'contentType' => 'application/json'])
}
}
You can find the available options here.
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. However, all major browsers require encryption when sending notifications.
When using the Push API, $key is the value of the getKey('p256dh') method, and $token is the value of the getKey('auth') method of the PushSubscription interface.
To delete a subscription, use the deletePushSubscription($endpoint) method on your user:
$user = \App\User::find(1);
$user->deletePushSubscription($endpoint);
See the Push API browser compatibility.
Please see CHANGELOG for more information about what has changed recently.
composer test
If you discover any security-related issues, please email [email protected] instead of using the issue tracker.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.