1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\OneSignal; |
4
|
|
|
|
5
|
|
|
use Berkayk\OneSignal\OneSignalClient; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use Illuminate\Notifications\Notification; |
8
|
|
|
use NotificationChannels\OneSignal\Exceptions\CouldNotSendNotification; |
9
|
|
|
|
10
|
|
|
class OneSignalChannel |
11
|
|
|
{ |
12
|
|
|
/** @var OneSignalClient */ |
13
|
|
|
protected $oneSignal; |
14
|
|
|
|
15
|
5 |
|
public function __construct(OneSignalClient $oneSignal) |
16
|
|
|
{ |
17
|
5 |
|
$this->oneSignal = $oneSignal; |
18
|
5 |
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Send the given notification. |
22
|
|
|
* |
23
|
|
|
* @param mixed $notifiable |
24
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
25
|
|
|
* |
26
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
27
|
|
|
* @throws \NotificationChannels\OneSignal\Exceptions\CouldNotSendNotification |
28
|
|
|
*/ |
29
|
5 |
|
public function send($notifiable, Notification $notification) |
30
|
|
|
{ |
31
|
5 |
|
if (! $userIds = $notifiable->routeNotificationFor('OneSignal')) { |
32
|
1 |
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
4 |
|
$payload = $notification->toOneSignal($notifiable)->toArray(); |
|
|
|
|
36
|
|
|
|
37
|
4 |
|
if (is_array($userIds)) { |
38
|
2 |
|
if (array_key_exists('email', $userIds)) { |
39
|
1 |
|
$payload['filters'] = collect([['field' => 'email', 'value' => $userIds['email']]]); |
40
|
1 |
|
} elseif (array_key_exists('tags', $userIds)) { |
41
|
2 |
|
$payload['tags'] = collect([$userIds['tags']]); |
42
|
|
|
} |
43
|
|
|
} else { |
44
|
2 |
|
$payload['include_player_ids'] = collect($userIds); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** @var ResponseInterface $response */ |
48
|
4 |
|
$response = $this->oneSignal->sendNotificationCustom( |
49
|
4 |
|
$this->payload($notifiable, $notification, $userIds) |
50
|
|
|
); |
51
|
|
|
|
52
|
4 |
|
if ($response->getStatusCode() !== 200) { |
53
|
1 |
|
throw CouldNotSendNotification::serviceRespondedWithAnError($response); |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
return $response; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param mixed $notifiable |
61
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
62
|
|
|
* @param mixed $targeting |
|
|
|
|
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
4 |
|
protected function payload($notifiable, $notification, $userIds) |
67
|
|
|
{ |
68
|
4 |
|
return OneSignalPayloadFactory::make($notifiable, $notification, $userIds); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.