1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\WebPush; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Log; |
6
|
|
|
use Minishlink\WebPush\WebPush; |
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
|
2 |
|
public function __construct(WebPush $webPush) |
19
|
|
|
{ |
20
|
2 |
|
$this->webPush = $webPush; |
21
|
2 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Send the given notification. |
25
|
|
|
* |
26
|
|
|
* @param mixed $notifiable |
27
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
2 |
|
public function send($notifiable, Notification $notification) |
31
|
|
|
{ |
32
|
2 |
|
$subscriptions = $notifiable->routeNotificationFor('WebPush'); |
33
|
|
|
|
34
|
2 |
|
if (! $subscriptions || $subscriptions->isEmpty()) { |
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
$payload = json_encode($notification->toWebPush($notifiable, $notification)->toArray()); |
|
|
|
|
39
|
|
|
|
40
|
2 |
|
$subscriptions->each(function ($sub) use ($payload) { |
41
|
2 |
|
$this->webPush->sendNotification( |
42
|
2 |
|
$sub->endpoint, |
43
|
2 |
|
$payload, |
44
|
2 |
|
$sub->public_key, |
45
|
2 |
|
$sub->auth_token |
46
|
|
|
); |
47
|
2 |
|
}); |
48
|
|
|
|
49
|
2 |
|
$response = $this->webPush->flush(); |
50
|
|
|
|
51
|
2 |
|
$this->logErrorsInDebug($response, $subscriptions, $payload); |
52
|
|
|
|
53
|
2 |
|
$this->deleteInvalidSubscriptions($response, $subscriptions); |
54
|
2 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array|bool $response |
58
|
|
|
* @param \Illuminate\Database\Eloquent\Collection $subscriptions |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
2 |
|
protected function deleteInvalidSubscriptions($response, $subscriptions) |
62
|
|
|
{ |
63
|
2 |
|
if (! is_array($response)) { |
64
|
1 |
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
foreach ($response as $index => $value) { |
68
|
1 |
|
if (! $value['success'] && isset($subscriptions[$index])) { |
69
|
1 |
|
$subscriptions[$index]->delete(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
1 |
|
} |
73
|
|
|
|
74
|
2 |
|
protected function logErrorsInDebug($response, $subscriptions, $payload) |
75
|
|
|
{ |
76
|
2 |
|
if(config('webpush.enable_logging')){ |
77
|
|
|
if(is_array($response)){ |
78
|
|
|
foreach($response as $index => $push){ |
79
|
|
|
if(!$push['success']){ |
80
|
|
|
Log::error("[WebPush] Error pushing: {$push['message']}\nEndpoint: {$push['endpoint']}\nPayload: {$payload}"); |
81
|
|
|
}else{ |
82
|
|
|
Log::info("[WebPush] Push successful\nEndpoint: {$subscriptions[$index]->endpoint}"); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
}elseif(is_bool($response) && $response === true){ |
86
|
|
|
Log::info('[WebPush] All messages successfully pushed'); |
87
|
|
|
}elseif(is_bool($response) && $response === false){ |
88
|
|
|
Log::info('[WebPush] No notifications in the queue'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
92
|
2 |
|
} |
93
|
|
|
} |
94
|
|
|
|
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.