|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\IonicPushNotifications; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use Illuminate\Notifications\Notification; |
|
7
|
|
|
use NotificationChannels\IonicPushNotifications\Exceptions\InvalidConfiguration; |
|
8
|
|
|
use NotificationChannels\IonicPushNotifications\Exceptions\CouldNotSendNotification; |
|
9
|
|
|
|
|
10
|
|
|
class IonicPushChannel |
|
11
|
|
|
{ |
|
12
|
|
|
const API_ENDPOINT = 'https://api.ionic.io/push/notifications'; |
|
13
|
|
|
|
|
14
|
|
|
/** @var Client */ |
|
15
|
|
|
protected $client; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param Client $client |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct(Client $client) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->client = $client; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Send the given notification. |
|
27
|
|
|
* |
|
28
|
|
|
* @param mixed $notifiable |
|
29
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
|
30
|
|
|
* |
|
31
|
|
|
* @throws \NotificationChannels\IonicPushNotifications\Exceptions\InvalidConfiguration |
|
32
|
|
|
* @throws \NotificationChannels\IonicPushNotifications\Exceptions\CouldNotSendNotification |
|
33
|
|
|
*/ |
|
34
|
|
|
public function send($notifiable, Notification $notification) |
|
35
|
|
|
{ |
|
36
|
|
|
$routing = collect($notifiable->routeNotificationFor('IonicPush')); |
|
37
|
|
|
// remove empty device tokens |
|
38
|
|
|
$routing->filter(function ($token) { |
|
39
|
|
|
return ! empty($token); |
|
40
|
|
|
}); |
|
41
|
|
|
// if there are no valid device tokens then do not send the notification |
|
42
|
|
|
if (! $routing->count() > 0) { |
|
43
|
|
|
return; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$authorizationKey = config('services.ionicpush.key'); |
|
47
|
|
|
|
|
48
|
|
|
if (is_null($authorizationKey)) { |
|
49
|
|
|
throw InvalidConfiguration::configurationNotSet(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$message = $notification->toIonicPush($notifiable); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
$ionicPushData = array_merge( |
|
55
|
|
|
[$message->getSendToType() => $routing->all()], |
|
56
|
|
|
$message->toArray() |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
$response = $this->client->post(self::API_ENDPOINT, [ |
|
60
|
|
|
'body' => json_encode($ionicPushData), |
|
61
|
|
|
'headers' => [ |
|
62
|
|
|
'Authorization' => "Bearer {$authorizationKey}", |
|
63
|
|
|
'Content-Type' => 'application/json', |
|
64
|
|
|
], |
|
65
|
|
|
]); |
|
66
|
|
|
|
|
67
|
|
|
if ($response->getStatusCode() !== 201) { |
|
68
|
|
|
throw CouldNotSendNotification::serviceRespondedWithAnError($response); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $response; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
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.