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
|
|
|
/** @var \Minishlink\WebPush\WebPush */ |
12
|
|
|
protected $webPush; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param \Minishlink\WebPush\WebPush $webPush |
16
|
|
|
* @return void |
|
|
|
|
17
|
|
|
*/ |
18
|
3 |
|
public function __construct(WebPush $webPush) |
19
|
|
|
{ |
20
|
3 |
|
$this->webPush = $webPush; |
21
|
3 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Send the given notification. |
25
|
|
|
* |
26
|
|
|
* @param mixed $notifiable |
27
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
3 |
|
public function send($notifiable, Notification $notification) |
31
|
|
|
{ |
32
|
3 |
|
$subscriptions = $notifiable->routeNotificationFor('WebPush'); |
33
|
|
|
|
34
|
3 |
|
if (! $subscriptions || $subscriptions->isEmpty()) { |
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** @var \NotificationChannels\WebPush\WebPushMessage $webPushMessage */ |
39
|
3 |
|
$webPushMessage = $notification->toWebPush($notifiable, $notification); |
40
|
3 |
|
$options = $webPushMessage->getOptions(); |
41
|
3 |
|
$payload = json_encode($webPushMessage->toArray()); |
42
|
|
|
|
43
|
3 |
|
$subscriptions->each(function (PushSubscription $pushSubscription) use ($payload, $options) { |
44
|
3 |
|
$this->webPush->sendNotification(new Subscription( |
45
|
3 |
|
$pushSubscription->endpoint, |
46
|
3 |
|
$pushSubscription->public_key, |
47
|
3 |
|
$pushSubscription->auth_token, |
48
|
3 |
|
$pushSubscription->content_encoding |
49
|
3 |
|
), $payload, false, $options); |
50
|
3 |
|
}); |
51
|
|
|
|
52
|
3 |
|
$reports = $this->webPush->flush(); |
53
|
|
|
|
54
|
3 |
|
$this->deleteInvalidSubscriptions($reports, $subscriptions); |
55
|
3 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param \Minishlink\WebPush\MessageSentReport[] $reports |
59
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $subscriptions |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
3 |
|
protected function deleteInvalidSubscriptions($reports, $subscriptions) |
63
|
|
|
{ |
64
|
3 |
|
foreach ($reports as $report) { |
65
|
3 |
|
if (is_null($report) || $report->isSuccess()) { |
66
|
3 |
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/* @var \Minishlink\WebPush\MessageSentReport $report */ |
70
|
1 |
|
$subscriptions->each(function ($subscription) use ($report) { |
71
|
1 |
|
if ($subscription->endpoint === $report->getEndpoint()) { |
72
|
1 |
|
logger()->warning('deleting subscription cause of '.$report->getReason()); |
73
|
1 |
|
$subscription->delete(); |
74
|
|
|
} |
75
|
1 |
|
}); |
76
|
|
|
} |
77
|
3 |
|
} |
78
|
|
|
} |
79
|
|
|
|
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.