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