Giter VIP home page Giter VIP logo

laravel-web-push's Introduction

Send Push Notification in Laravel

Total Downloads Coverage Status CI

More info

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.

Installation

composer require alexlisenkov/laravel-web-push
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

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.

<?php
/*
 * To generate a application server keys
 * Visit: https://web-push-codelab.glitch.me/
 */

return [
    'public_key' => '',
    '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 or the Living Standards to see all available options.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Response;
use AlexLisenkov\LaravelWebPush\PushMessage;
use AlexLisenkov\LaravelWebPush\PushSubscription;

class PushMessageController
{
    public function sendPushMessage(): Response
    {
        // Create a subscription
        $subscription = new PushSubscription(
            "endpoint",
            "p256dh",
            "auth"
        );
        
        // Create a message
        $message = new PushMessage();
        $message->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 or the Living Standards to see all available options.

<?php

namespace App\Http\Controllers;

use AlexLisenkov\LaravelWebPush\PushMessage;

class ExampleMessage extends PushMessage
{
    protected $title = 'Hello world';

    protected $body = 'This message is sent using web push';

    protected $icon = 'https://placekitten.com/75/75';
    
    // Or override a getter
    public function getData()
    {
        return User()->name;
    }
}

Creating a subscription

The AlexLisenkov\LaravelWebPush\PushSubscription is used to create a new subscription.

<?php
use AlexLisenkov\LaravelWebPush\PushSubscription;

new PushSubscription(
        "endpoint",
        "p256dh",
        "auth"
    );

It is also possible for you to implement AlexLisenlov\LaravelWebPush\Contracts\PushSubscriptionContract into any class. For example on a model.

Sending a notification

Now that we have a subscriber and a message, we can send it out.

<?php
use AlexLisenkov\LaravelWebPush\PushSubscription;

// Create a new message
$message = new ExampleMessage();

// Create a new subscription
$subscription = new PushSubscription(
        "endpoint",
        "p256dh",
        "auth"
    );

// We can either use the message to send it to a subscription
$message->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.

self.addEventListener('push', function(e) {
    let data = e.data.json();
    
    self.registration.showNotification(data.title, data.options);
});

Testing

$ composer test

Security

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

Contributing

Contributions are welcome.

Credits

License

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.