1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\WebPush;
|
4
|
|
|
|
5
|
|
|
use Illuminate\Notifications\Notification;
|
6
|
|
|
use Minishlink\WebPush\Subscription;
|
7
|
|
|
use Minishlink\WebPush\WebPush;
|
8
|
|
|
|
9
|
|
|
class WebPushChannel
|
10
|
|
|
{
|
11
|
|
|
/**
|
12
|
|
|
* @var \Minishlink\WebPush\WebPush
|
13
|
|
|
*/
|
14
|
|
|
protected $webPush;
|
15
|
|
|
|
16
|
|
|
/**
|
17
|
|
|
* @var \NotificationChannels\WebPush\ReportHandlerInterface
|
18
|
|
|
*/
|
19
|
|
|
protected $reportHandler;
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* @param \Minishlink\WebPush\WebPush $webPush
|
23
|
|
|
* @param \NotificationChannels\WebPush\ReportHandlerInterface $webPush
|
24
|
|
|
* @return void
|
|
|
|
|
25
|
|
|
*/
|
26
|
2 |
|
public function __construct(WebPush $webPush, ReportHandlerInterface $reportHandler)
|
27
|
|
|
{
|
28
|
2 |
|
$this->webPush = $webPush;
|
29
|
2 |
|
$this->reportHandler = $reportHandler;
|
30
|
2 |
|
}
|
31
|
|
|
|
32
|
|
|
/**
|
33
|
|
|
* Send the given notification.
|
34
|
|
|
*
|
35
|
|
|
* @param mixed $notifiable
|
36
|
|
|
* @param \Illuminate\Notifications\Notification $notification
|
37
|
|
|
* @return void
|
38
|
|
|
*/
|
39
|
|
|
public function send($notifiable, Notification $notification)
|
40
|
|
|
{
|
41
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection $subscriptions */
|
42
|
|
|
$subscriptions = $notifiable->routeNotificationFor('WebPush', $notification);
|
43
|
|
|
|
44
|
|
|
if (empty($subscriptions)) {
|
45
|
|
|
return;
|
46
|
|
|
}
|
47
|
|
|
|
48
|
|
|
/** @var \NotificationChannels\WebPush\WebPushMessage $message */
|
49
|
|
|
$message = $notification->toWebPush($notifiable, $notification);
|
|
|
|
|
50
|
|
|
$payload = json_encode($message->toArray());
|
51
|
|
|
$options = $message->getOptions();
|
52
|
|
|
|
53
|
|
|
/** @var \NotificationChannels\WebPush\PushSubscription $subscription */
|
54
|
|
|
foreach ($subscriptions as $subscription) {
|
55
|
|
|
$this->webPush->sendNotification(new Subscription(
|
56
|
|
|
$subscription->endpoint,
|
57
|
|
|
$subscription->public_key,
|
58
|
|
|
$subscription->auth_token,
|
59
|
|
|
$subscription->content_encoding
|
60
|
|
|
), $payload, false, $options);
|
61
|
|
|
}
|
62
|
|
|
|
63
|
|
|
$reports = $this->webPush->flush();
|
64
|
|
|
|
65
|
|
|
$this->handleReports($reports, $subscriptions, $message);
|
66
|
|
|
}
|
67
|
|
|
|
68
|
|
|
/**
|
69
|
|
|
* Handle the reports.
|
70
|
|
|
*
|
71
|
|
|
* @param \Generator $reports
|
72
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $subscriptions
|
73
|
|
|
* @param \NotificationChannels\WebPush\WebPushMessage $message
|
74
|
|
|
* @return void
|
75
|
|
|
*/
|
76
|
|
|
protected function handleReports($reports, $subscriptions, $message)
|
77
|
|
|
{
|
78
|
|
|
/** @var \Minishlink\WebPush\MessageSentReport $report */
|
79
|
|
|
foreach ($reports as $report) {
|
80
|
|
|
if ($report && $subscription = $this->findSubscription($subscriptions, $report)) {
|
|
|
|
|
81
|
|
|
$this->reportHandler->handleReport($report, $subscription, $message);
|
82
|
|
|
}
|
83
|
|
|
}
|
84
|
|
|
}
|
85
|
|
|
|
86
|
|
|
/**
|
87
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $subscriptions
|
88
|
|
|
* @param \Minishlink\WebPush\MessageSentReport $report
|
89
|
|
|
* @return void
|
90
|
|
|
*/
|
91
|
|
|
protected function findSubscription($subscriptions, $report)
|
92
|
|
|
{
|
93
|
|
|
foreach ($subscriptions as $subscription) {
|
94
|
|
|
if ($subscription->endpoint === $report->getEndpoint()) {
|
95
|
|
|
return $subscription;
|
96
|
|
|
}
|
97
|
|
|
}
|
98
|
|
|
}
|
99
|
|
|
}
|
100
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.