|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NotificationChannels\Gcm; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Events\Dispatcher; |
|
7
|
|
|
use Illuminate\Notifications\Events\NotificationFailed; |
|
8
|
|
|
use Illuminate\Notifications\Notification; |
|
9
|
|
|
use NotificationChannels\Gcm\Exceptions\SendingFailed; |
|
10
|
|
|
use ZendService\Google\Gcm\Client; |
|
11
|
|
|
|
|
12
|
|
|
class GcmChannel |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var Client */ |
|
15
|
|
|
protected $client; |
|
16
|
|
|
|
|
17
|
|
|
/** @var Dispatcher */ |
|
18
|
|
|
protected $events; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param Client $client |
|
22
|
|
|
* @param Dispatcher $events |
|
23
|
|
|
*/ |
|
24
|
1 |
|
public function __construct(Client $client, Dispatcher $events) |
|
25
|
|
|
{ |
|
26
|
1 |
|
$this->client = $client; |
|
27
|
1 |
|
$this->events = $events; |
|
28
|
1 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Send the notification to Google Cloud Messaging. |
|
32
|
|
|
* |
|
33
|
|
|
* @param mixed $notifiable |
|
34
|
|
|
* @param Notification $notification |
|
35
|
|
|
* @return void |
|
36
|
|
|
* |
|
37
|
|
|
* @throws Exceptions\SendingFailed |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function send($notifiable, Notification $notification) |
|
40
|
|
|
{ |
|
41
|
1 |
|
$tokens = (array) $notifiable->routeNotificationFor('gcm'); |
|
42
|
1 |
|
if (! $tokens) { |
|
|
|
|
|
|
43
|
1 |
|
return; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$message = $notification->toGcm($notifiable); |
|
47
|
|
|
if (! $message) { |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$packet = $this->getPacket($tokens, $message); |
|
52
|
|
|
|
|
53
|
|
|
try { |
|
54
|
|
|
$response = $this->client->send($packet); |
|
55
|
|
|
} catch (Exception $exception) { |
|
56
|
|
|
throw SendingFailed::create($exception); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (! $response->getFailureCount() == 0) { |
|
60
|
|
|
$this->handleFailedNotifications($notifiable, $notification, $response); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param $tokens |
|
66
|
|
|
* @param $message |
|
67
|
|
|
* |
|
68
|
|
|
* @return \NotificationChannels\Gcm\Packet |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function getPacket($tokens, $message) |
|
71
|
|
|
{ |
|
72
|
|
|
$packet = new Packet(); |
|
73
|
|
|
|
|
74
|
|
|
$packet->setRegistrationIds($tokens); |
|
75
|
|
|
$packet->setCollapseKey(str_slug($message->title)); |
|
76
|
|
|
$packet->setData([ |
|
77
|
|
|
'title' => $message->title, |
|
78
|
|
|
'message' => $message->message, |
|
79
|
|
|
] + $message->data); |
|
80
|
|
|
$packet->setNotification([ |
|
81
|
|
|
'title' => $message->title, |
|
82
|
|
|
'message' => $message->message, |
|
83
|
|
|
] + $message->data); |
|
84
|
|
|
|
|
85
|
|
|
return $packet; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param $notifiable |
|
90
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
|
91
|
|
|
* @param $response |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function handleFailedNotifications($notifiable, Notification $notification, $response) |
|
94
|
|
|
{ |
|
95
|
|
|
$results = $response->getResults(); |
|
96
|
|
|
|
|
97
|
|
|
foreach ($results as $token => $result) { |
|
98
|
|
|
if (! isset($result['error'])) { |
|
99
|
|
|
continue; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->events->fire( |
|
103
|
|
|
new NotificationFailed($notifiable, $notification, $this, [ |
|
|
|
|
|
|
104
|
|
|
'token' => $token, |
|
105
|
|
|
'error' => $result['error'], |
|
106
|
|
|
]) |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.