PHP code example of alexlisenkov / laravel-web-push

1. Go to this page and download the library: Download alexlisenkov/laravel-web-push library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alexlisenkov / laravel-web-push example snippets



/*
 * 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,
];



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




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



use AlexLisenkov\LaravelWebPush\PushSubscription;

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


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);
bash
php artisan vendor:publish --provider="AlexLisenkov\LaravelWebPush\LaravelWebPushServiceProvider"