|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\OneSignal; |
|
4
|
|
|
|
|
5
|
|
|
use Berkayk\OneSignal\OneSignalClient; |
|
6
|
|
|
use NotificationChannels\OneSignal\Exceptions\CouldNotSendNotification; |
|
7
|
|
|
use NotificationChannels\OneSignal\Events\MessageWasSent; |
|
8
|
|
|
use NotificationChannels\OneSignal\Events\SendingMessage; |
|
9
|
|
|
use Illuminate\Notifications\Notification; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
|
|
12
|
|
|
class OneSignalChannel |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var OneSignalClient */ |
|
15
|
|
|
protected $oneSignal; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(OneSignalClient $oneSignal) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->oneSignal = $oneSignal; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Send the given notification. |
|
24
|
|
|
* |
|
25
|
|
|
* @param mixed $notifiable |
|
26
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
|
27
|
|
|
* |
|
28
|
|
|
* @throws \NotificationChannels\OneSignal\Exceptions\CouldNotSendNotification |
|
29
|
|
|
*/ |
|
30
|
|
|
public function send($notifiable, Notification $notification) |
|
31
|
|
|
{ |
|
32
|
|
|
if (! $userIds = $notifiable->routeNotificationFor('OneSignal')) { |
|
33
|
|
|
return; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$shouldSendMessage = event(new SendingMessage($notifiable, $notification), [], true) !== false; |
|
37
|
|
|
|
|
38
|
|
|
if (! $shouldSendMessage) { |
|
39
|
|
|
return; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$payload = $notification->toOneSignal($notifiable)->toArray(); |
|
43
|
|
|
$payload['include_player_ids'] = collect($userIds); |
|
44
|
|
|
|
|
45
|
|
|
/** @var ResponseInterface $response */ |
|
46
|
|
|
$response = $this->oneSignal->sendNotificationCustom($payload); |
|
47
|
|
|
|
|
48
|
|
|
if ($response->getStatusCode() !== 200) { |
|
49
|
|
|
throw CouldNotSendNotification::serviceRespondedWithAnError($response); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
event(new MessageWasSent($notifiable, $notification)); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|