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