2017 © Pedro Peláez
 

library webpush

Web Push Notifications driver for Laravel.

image

bosym/webpush

Web Push Notifications driver for Laravel.

  • Saturday, December 16, 2017
  • by MCMainiac
  • Repository
  • 0 Watchers
  • 1 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 31 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

Web push notifications channel for Laravel

Latest Version on Packagist Software License Build Status StyleCI SensioLabsInsight Quality Score Code Coverage Total Downloads, (*1)

This package makes it easy to send web push notifications with Laravel., (*2)

Installation

You can install the package via composer:, (*3)

``` bash composer require laravel-notification-channels/webpush, (*4)


First you must install the service provider (skip for Laravel>=5.5): ``` php // config/app.php 'providers' => [ ... NotificationChannels\WebPush\WebPushServiceProvider::class, ],

Add the NotificationChannels\WebPush\HasPushSubscriptions trait to your User model:, (*5)

``` php use NotificationChannels\WebPush\HasPushSubscriptions;, (*6)

class User extends Model { use HasPushSubscriptions; }, (*7)


Next publish the migration with: ``` bash php artisan vendor:publish --provider="NotificationChannels\WebPush\WebPushServiceProvider" --tag="migrations"

Run the migrate command to create the necessary table:, (*8)

``` bash php artisan migrate, (*9)


You can also publish the config file with: ``` bash php artisan vendor:publish --provider="NotificationChannels\WebPush\WebPushServiceProvider" --tag="config"

Generate the VAPID keys with (required for browser authentication) with:, (*10)

``` bash php artisan webpush:vapid, (*11)


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](https://console.cloud.google.com) 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: ``` php 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'); // ->data(['id' => $notification->id]) // ->badge() // ->dir() // ->image() // ->lang() // ->renotify() // ->requireInteraction() // ->tag() // ->vibrate() } }

Save/Update Subscriptions

To save or update a subscription use the updatePushSubscription($endpoint, $key = null, $token = null) method on your user:, (*12)

``` php $user = \App\User::find(1);, (*13)

$user->updatePushSubscription($endpoint, $key, $token);, (*14)


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: ``` php $user = \App\User::find(1); $user->deletePushSubscription($endpoint);

Demo

For a complete implementation with a Service Worker check this demo., (*15)

Browser Compatibility

The Push API currently works on Chrome and Firefox., (*16)

Changelog

Please see CHANGELOG for more information what has changed recently., (*17)

Testing

bash $ composer test, (*18)

Security

If you discover any security related issues, please email themsaid@gmail.com instead of using the issue tracker., (*19)

Contributing

Please see CONTRIBUTING for details., (*20)

Credits

License

The MIT License (MIT). Please see License File for more information., (*21)

The Versions