2017 © Pedro Peláez
 

library webpush

Web Push Notifications driver for Laravel.

image

laravel-notification-channels/webpush

Web Push Notifications driver for Laravel.

  • Thursday, April 5, 2018
  • by laravel-notification-channels
  • Repository
  • 15 Watchers
  • 162 Stars
  • 26,295 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 28 Forks
  • 4 Open issues
  • 12 Versions
  • 22 % Grown

The README.md

Web push notifications channel for Laravel

Latest Version on Packagist Build Status 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 add the `NotificationChannels\WebPush\HasPushSubscriptions` trait to your `User` model: ``` php use NotificationChannels\WebPush\HasPushSubscriptions; class User extends Model { use HasPushSubscriptions; }

Next publish the migration with:, (*5)

``` bash php artisan vendor:publish --provider="NotificationChannels\WebPush\WebPushServiceProvider" --tag="migrations", (*6)


Run the migrate command to create the necessary table: ``` bash php artisan migrate

You can also publish the config file with:, (*7)

``` bash php artisan vendor:publish --provider="NotificationChannels\WebPush\WebPushServiceProvider" --tag="config", (*8)


Generate the VAPID keys (required for browser authentication) with: ``` bash php artisan webpush:vapid

This command will set VAPID_PUBLIC_KEY and VAPID_PRIVATE_KEYin your .env file., (*9)

Note: if targeting Safari or iOS after 2023, you will need to include the VAPID_SUBJECT variable as well or Apple will return a BadJwtToken error., (*10)

These keys must be safely stored and should not change., (*11)

If you still want support for Google Cloud Messaging, set the GCM_KEY and GCM_SENDER_ID in your .env file., (*12)

Usage

Now you can use the channel in your via() method inside the notification as well as send a web push notification:, (*13)

``` php use Illuminate\Notifications\Notification; use NotificationChannels\WebPush\WebPushMessage; use NotificationChannels\WebPush\WebPushChannel;, (*14)

class AccountApproved extends Notification { public function via($notifiable) { return [WebPushChannel::class]; }, (*15)

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()
}

}, (*16)


You can find the available options [here](https://github.com/web-push-libs/web-push-php#notifications-and-default-options). ### Save/Update Subscriptions To save or update a subscription use the `updatePushSubscription($endpoint, $key = null, $token = null, $contentEncoding = null)` method on your user: ``` php $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., (*17)

Delete Subscriptions

To delete a subscription use the deletePushSubscription($endpoint) method on your user:, (*18)

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

$user->deletePushSubscription($endpoint);, (*20)


## Browser Compatibility See the [Push API](https://caniuse.com/#feat=push-api) browser compatibility. ## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information about what has changed recently. ## Testing ``` bash $ composer test

Security

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

Contributing

Please see CONTRIBUTING for details., (*22)

Credits

License

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

The Versions