Webpush notifications channel for Laravel 5.3
PHP
Latest commit 3b0c60f Nov 15, 2016 @cretueusebiu cretueusebiu Fix version
Permalink
Failed to load latest commit information.
migrations initial Aug 12, 2016
src remove events since they are now fired in laravel core Aug 16, 2016
tests Applied fixes from StyleCI Aug 14, 2016
.editorconfig initial Aug 12, 2016
.gitattributes initial Aug 12, 2016
.gitignore Add tests Aug 14, 2016
.scrutinizer.yml Add code coverage (#9) Aug 16, 2016
.styleci.yml initial Aug 12, 2016
.travis.yml Add code coverage (#9) Aug 16, 2016
CHANGELOG.md initial Aug 12, 2016
CONTRIBUTING.md initial Aug 12, 2016
LICENSE.md initial Aug 12, 2016
README.md Update README.md Oct 27, 2016
composer.json Fix version Nov 14, 2016
phpunit.xml.dist initial Aug 12, 2016

README.md

Web push notifications channel for Laravel 5.3

Latest Version on Packagist Software License Build Status StyleCI SensioLabsInsight Quality Score Code Coverage Total Downloads

This package makes it easy to send web push notifications with Laravel 5.3.

Installation

You can install the package via composer:

composer require laravel-notification-channels/webpush

First you must install the service provider:

// config/app.php
'providers' => [
    ...
    NotificationChannels\WebPush\WebPushServiceProvider::class,
],

Then configure Google Cloud Messaging by setting your key and sender_id:

// config/services.php
'gcm' => [
    'key' => '',
    'sender_id' => ',
],

You need to add the NotificationChannels\WebPush\HasPushSubscriptions in 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"

After the migration has been published you can create the push_subscriptions table by running the migrations:

php artisan migrate

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);

Demo

For a complete implementation with a Service Worker check this demo.

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

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.