-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(notification): web push notification support
- Loading branch information
1 parent
c19415e
commit 1eb79e1
Showing
17 changed files
with
95 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
use Domain\Authentication\Entity\User; | ||
use Domain\Notification\Entity\Notification; | ||
use Domain\Notification\Entity\PushSubscription; | ||
use Domain\Notification\Repository\PushSubscriptionRepositoryInterface; | ||
use Infrastructure\Notification\Doctrine\Repository\PushSubscriptionRepository; | ||
use Minishlink\WebPush\Subscription; | ||
use Minishlink\WebPush\WebPush; | ||
use Psr\Log\LoggerInterface; | ||
|
@@ -24,23 +24,23 @@ final class WebPushService | |
private ?string $icon; | ||
|
||
public function __construct( | ||
private readonly PushSubscriptionRepositoryInterface $repository, | ||
private readonly PushSubscriptionRepository $repository, | ||
private readonly LoggerInterface $logger, | ||
RequestStack $stack | ||
) { | ||
$this->webPush = new WebPush([ | ||
'VAPID' => [ | ||
'subject' => 'mailto:[email protected]', | ||
'publicKey' => $_ENV['VAPID_PUBLIC_KEY'], | ||
'privateKey' => $_ENV['VAPID_PRIVATE_KEY'] | ||
] | ||
'privateKey' => $_ENV['VAPID_PRIVATE_KEY'], | ||
], | ||
]); | ||
$this->icon = $stack->getCurrentRequest()?->getUriForPath('/images/logo_icon.png'); | ||
} | ||
|
||
public function notifyChannel(Notification $notification): void | ||
{ | ||
/** @var PushSubscription[] $subscription */ | ||
/** @var PushSubscription[] $subscriptions */ | ||
$subscriptions = $this->repository->findAll(); | ||
|
||
try { | ||
|
@@ -49,11 +49,8 @@ public function notifyChannel(Notification $notification): void | |
} | ||
|
||
foreach ($this->webPush->flush() as $report) { | ||
if (!$report->isSuccess()) { | ||
$this->repository->delete($subscription); | ||
dd($report); | ||
} else { | ||
dd($report); | ||
if (! $report->isSuccess()) { | ||
$this->repository->deleteSubscriptionByEndpoint($report->getEndpoint()); | ||
} | ||
} | ||
} catch (\Throwable $e) { | ||
|
@@ -64,17 +61,19 @@ public function notifyChannel(Notification $notification): void | |
|
||
public function notifyUser(Notification $notification, User $user): void | ||
{ | ||
/** @var PushSubscription[] $subscription */ | ||
$subscriptions = $this->repository->findBy(['user' => $user]); | ||
/** @var PushSubscription[] $subscriptions */ | ||
$subscriptions = $this->repository->findBy([ | ||
'user' => $user, | ||
]); | ||
|
||
try { | ||
foreach ($subscriptions as $subscription) { | ||
$this->push($subscription, $notification); | ||
} | ||
|
||
foreach ($this->webPush->flush() as $report) { | ||
if (!$report->isSuccess()) { | ||
$this->repository->delete($subscription); | ||
if (! $report->isSuccess()) { | ||
$this->repository->deleteSubscriptionByEndpoint($report->getEndpoint()); | ||
} | ||
} | ||
} catch (\Throwable $e) { | ||
|
@@ -90,25 +89,28 @@ private function push(PushSubscription $subscription, Notification $notification | |
$this->webPush->queueNotification( | ||
subscription: Subscription::create([ | ||
'endpoint' => $subscription->getEndpoint(), | ||
'publicKey' => $subscription->getKeys()->p256dh, | ||
'authToken' => $subscription->getKeys()->auth | ||
'publicKey' => $subscription->getKeys()?->p256dh, | ||
'authToken' => $subscription->getKeys()?->auth, | ||
]), | ||
payload: json_encode([ | ||
'title' => 'UNH Rapport', | ||
'options' => [ | ||
'body' => $notification->getMessage(), | ||
'data' => [ | ||
"url" => $notification->getUrl(), | ||
'url' => $notification->getUrl(), | ||
], | ||
'actions' => [ | ||
["action" => "show", "title" => "Voir les détails"] | ||
[ | ||
'action' => 'show', | ||
'title' => 'Voir les détails', | ||
], | ||
], | ||
"icon" => $this->icon, | ||
'icon' => $this->icon, | ||
'requireInteraction' => true, | ||
'timestamp' => $notification->getCreatedAt()->format('u'), | ||
'lang' => 'FR' | ||
'timestamp' => $notification->getCreatedAt()?->format('u'), | ||
'lang' => 'FR', | ||
], | ||
]) | ||
]) ?: null | ||
); | ||
} | ||
} |
Oops, something went wrong.