Search…
The Web Push Service
The WebPush object requires a PSR-17 Request Factory, a PSR-18 Http Client and an Extension Manager.
1
use Nyholm\Psr7\Factory\Psr17Factory;
2
use Symfony\Component\HttpClient\Psr18Client;
3
use WebPush\WebPush;
4
5
$client = new Psr18Client();
6
$requestFactory = new Psr17Factory();
7
8
$service = new WebPush($client, $requestFactory, $extensionManager);
Copied!
The service is now ready to send Notifications to the Subscriptions. The StatusReport object that is returned is explained here.
1
<?php
2
3
use WebPush\Subscription;
4
use WebPush\Notification;
5
6
$subscription = Subscription::createFromString('{"endpoint":"https://updates.push.services.mozilla.com/wpush/v2/AAAAAAAA[…]AAAAAAAAA","keys":{"auth":"XXXXXXXXXXXXXX","p256dh":"YYYYYYYY[…]YYYYYYYYYYYYY"}}');
7
$notification = Notification::create()
8
->withPayload('Hello world')
9
;
10
11
$statusReport = $service->send($notification, $subscription);
Copied!
In this example, we load the Subscription object from a string, but usually to retrieve the Subscription objects from a database or a dedicated storage.
Export as PDF
Copy link