zupago / webpush
Web Push Notifications driver for Laravel.
Requires
- php: ^7.0
- illuminate/notifications: ^5.3
- illuminate/support: ^5.1
- minishlink/web-push: ^2.0
Requires (Dev)
- mockery/mockery: ~1.0
- orchestra/testbench: ^3.5
- phpunit/phpunit: ~6.0
This package is not auto-updated.
Last update: 2020-01-24 17:29:02 UTC
README
This package makes it easy to send web push notifications with Laravel.
Installation
You can install the package via composer:
composer require zupago/webpush
First you must install the service provider (skip for Laravel>=5.5):
// config/app.php 'providers' => [ ... NotificationChannels\webpush\webpushServiceProvider::class, ],
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 with (required for browser authentication) with:
php artisan webpush:vapid
This command will set VAPID_PUBLIC_KEY
and VAPID_PRIVATE_KEY
in your .env
file.
These keys must be safely stored and should not change.
If you still want support 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 webpushMessage::create() // ->id($notification->id) ->title('Approved!') ->icon('/approved-icon.png') ->body('Your account was approved!') ->action('View account', 'view_account'); } }
Save/Update Subscriptions
To save or update a subscription use the updatePushSubscription($endpoint, $key = null, $token = null)
method on your user:
$user = \App\User::find(1); $user->updatePushSubscription($endpoint, $key, $token);
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
The Push API currently works on Chrome and Firefox.
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test