forked from laravel-notification-channels/onesignal
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathOneSignalChannel.php
44 lines (36 loc) · 1.24 KB
/
OneSignalChannel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace NotificationChannels\OneSignal;
use Berkayk\OneSignal\OneSignalClient;
use NotificationChannels\OneSignal\Exceptions\CouldNotSendNotification;
use Illuminate\Notifications\Notification;
use Psr\Http\Message\ResponseInterface;
class OneSignalChannel
{
/** @var OneSignalClient */
protected $oneSignal;
public function __construct(OneSignalClient $oneSignal)
{
$this->oneSignal = $oneSignal;
}
/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
*
* @throws \NotificationChannels\OneSignal\Exceptions\CouldNotSendNotification
*/
public function send($notifiable, Notification $notification)
{
if (! $userIds = $notifiable->routeNotificationFor('OneSignal')) {
return;
}
$payload = $notification->toOneSignal($notifiable)->toArray();
$payload['include_player_ids'] = collect($userIds);
/** @var ResponseInterface $response */
$response = $this->oneSignal->sendNotificationCustom($payload);
if ($response->getStatusCode() !== 200) {
throw CouldNotSendNotification::serviceRespondedWithAnError($response);
}
}
}