Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/alexlisenkov/laravel-web-push

Laravel package for sending out push notifications
https://github.com/alexlisenkov/laravel-web-push

laravel package php push-notifications web-push

Last synced: 29 days ago
JSON representation

Laravel package for sending out push notifications

Awesome Lists containing this project

README

        

# Send Push Notification in Laravel
[![Total Downloads](https://poser.pugx.org/alexlisenkov/laravel-web-push/downloads)](https://packagist.org/packages/alexlisenkov/laravel-web-push)
[![Coverage Status](https://coveralls.io/repos/github/AlexLisenkov/laravel-web-push/badge.svg?branch=master)](https://coveralls.io/github/AlexLisenkov/laravel-web-push?branch=master)
![CI](https://github.com/AlexLisenkov/laravel-web-push/workflows/CI/badge.svg)

[![More info](https://developers.google.com/web/fundamentals/push-notifications/images/svgs/server-to-push-service.svg)](https://developers.google.com/web/fundamentals/push-notifications/web-push-protocol)

The `alexlisenkov/laravel-web-push` package is a package to send push notifications.
Send out push messages as a standalone package. Use this if you dont work with laravel notification channels.

If you are new to the Web Push Protocol please [read about the fundamentals](https://developers.google.com/web/fundamentals/push-notifications/web-push-protocol).

## Installation

```bash
composer require alexlisenkov/laravel-web-push
```

```bash
php artisan vendor:publish --provider="AlexLisenkov\LaravelWebPush\LaravelWebPushServiceProvider"
```

## Configuration
To send out Web Push notifications you need to generate yourself an identity.
The simplest thing to do is to visit [https://web-push-codelab.glitch.me](https://web-push-codelab.glitch.me/)

Open up `config/laravel-web-push.php`

Copy the public key and private key into your configuration. Please note that this public key is the same as you will use in the applicationServerKey in the [JavaScript pushManager api](https://developer.mozilla.org/en-US/docs/Web/API/PushManager).

```php
'',
'private_key' => '',
'subject' => config('APP_URL', 'mailto:[email protected]'),
'expiration' => 43200,
'TTL' => 2419200,
];
```

# Sending a Web Push

## Quick guide
A message can be created by creating a new `AlexLisenkov\LaravelWebPush\PushMessage` class.

Please see [this MDN doc](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification#Parameters) or the [Living Standards](https://notifications.spec.whatwg.org/#dictdef-notificationoptions) to see all available options.
```php
setTitle('Hello World');
$message->setBody('This message is sent using web push');
$message->setIcon('https://placekitten.com/75/75');

// We can either use the message to send it to a subscription
$message->sendTo($subscription)->wait();

// Or send the subscription a message
$subscription->sendMessage($message)->wait();

return response('ok');
}
}

```

## Creating message objects
A message can be created by creating a new class that extends the `AlexLisenkov\LaravelWebPush\PushMessage` class.
Please see [this MDN doc](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification#Parameters) or the [Living Standards](https://notifications.spec.whatwg.org/#dictdef-notificationoptions) to see all available options.
```php
name;
}
}

```

## Creating a subscription
The `AlexLisenkov\LaravelWebPush\PushSubscription` is used to create a new subscription.
```php
sendTo($subscription);

// Or send the subscription a message
$subscription->sendMessage($message);
```

## Service worker
Show a notification to the subscriber by adding an event listener in your service worker.
```js
self.addEventListener('push', function(e) {
let data = e.data.json();

self.registration.showNotification(data.title, data.options);
});
```
## Testing

``` bash
$ composer test
```

## Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

## Contributing

Contributions are welcome.

- [PSR-2 coding standards](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)
- Keep the project tested
- Keep your pull requests small and limited

## Credits

- [Alex Lisenkov](https://github.com/alexlisenkov)
- [All Contributors](../../contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE) for more information.