bentools/webpush-bundle
Symfony bundle to send Web Push notifications using the Web Push protocol. Manage user-to-subscription associations (multi-device and shared devices) with your own persistence (Doctrine or custom). Includes VAPID key generation and backend APIs for subscriptions.
First, you have to implement BenTools\WebPushBundle\Model\Subscription\UserSubscriptionInterface.
It's a simple entity which associates:
PushSubscription javascript object.You're free to use Doctrine or anything else.
Example class:
# src/Entity/UserSubscription.php
namespace App\Entity;
use BenTools\WebPushBundle\Model\Subscription\UserSubscriptionInterface;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* [@ORM](https://github.com/ORM)\Entity()
*/
class UserSubscription implements UserSubscriptionInterface
{
/**
* [@var](https://github.com/var) int
*
* [@ORM](https://github.com/ORM)\Id()
* [@ORM](https://github.com/ORM)\GeneratedValue(strategy="AUTO")
* [@ORM](https://github.com/ORM)\Column(type="integer")
*/
private $id;
/**
* [@var](https://github.com/var) User
* [@ORM](https://github.com/ORM)\ManyToOne(targetEntity="App\Entity\User")
* [@ORM](https://github.com/ORM)\JoinColumn(nullable=false)
*/
private $user;
/**
* [@var](https://github.com/var) string
*
* [@ORM](https://github.com/ORM)\Column(type="string")
*/
private $subscriptionHash;
/**
* [@var](https://github.com/var) array
*
* [@ORM](https://github.com/ORM)\Column(type="json")
*/
private $subscription;
/**
* UserSubscription constructor.
* [@param](https://github.com/param) User $user
* [@param](https://github.com/param) string $subscriptionHash
* [@param](https://github.com/param) array $subscription
*/
public function __construct(User $user, string $subscriptionHash, array $subscription)
{
$this->user = $user;
$this->subscriptionHash = $subscriptionHash;
$this->subscription = $subscription;
}
/**
* [@return](https://github.com/return) int
*/
public function getId(): ?int
{
return $this->id;
}
/**
* [@inheritDoc](https://github.com/inheritDoc)
*/
public function getUser(): UserInterface
{
return $this->user;
}
/**
* [@inheritDoc](https://github.com/inheritDoc)
*/
public function getSubscriptionHash(): string
{
return $this->subscriptionHash;
}
/**
* [@inheritDoc](https://github.com/inheritDoc)
*/
public function getEndpoint(): string
{
return $this->subscription['endpoint'];
}
/**
* [@inheritDoc](https://github.com/inheritDoc)
*/
public function getPublicKey(): string
{
return $this->subscription['keys']['p256dh'];
}
/**
* [@inheritDoc](https://github.com/inheritDoc)
*/
public function getAuthToken(): string
{
return $this->subscription['keys']['auth'];
}
/**
* Content-encoding (default: aesgcm).
*
* [@return](https://github.com/return) string
*/
public function getContentEncoding(): string
{
return $this->subscription['content-encoding'] ?? 'aesgcm';
}
}
Previous: Installation
How can I help you explore Laravel packages today?