1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Gcm; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Events\Dispatcher; |
7
|
|
|
use ZendService\Google\Gcm\Client; |
8
|
|
|
use Illuminate\Notifications\Notification; |
9
|
|
|
use NotificationChannels\Gcm\Exceptions\SendingFailed; |
10
|
|
|
use Illuminate\Notifications\Events\NotificationFailed; |
11
|
|
|
|
12
|
|
|
class GcmChannel |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The GCM client instance. |
16
|
|
|
* |
17
|
|
|
* @var \ZendService\Google\Gcm\Client |
18
|
|
|
*/ |
19
|
|
|
protected $client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The event dispatcher instance. |
23
|
|
|
* |
24
|
|
|
* @var \Illuminate\Events\Dispatcher |
25
|
|
|
*/ |
26
|
|
|
protected $events; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Create a new channel instance. |
30
|
|
|
* |
31
|
|
|
* @param \ZendService\Google\Gcm\Client $client |
32
|
|
|
* @param \Illuminate\Events\Dispatcher $events |
33
|
|
|
*/ |
34
|
1 |
|
public function __construct(Client $client, Dispatcher $events) |
35
|
|
|
{ |
36
|
1 |
|
$this->client = $client; |
37
|
1 |
|
$this->events = $events; |
38
|
1 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Send the notification to Google Cloud Messaging. |
42
|
|
|
* |
43
|
|
|
* @param mixed $notifiable |
44
|
|
|
* @param Notification $notification |
45
|
|
|
* @return void |
46
|
|
|
* @throws Exceptions\SendingFailed |
47
|
|
|
*/ |
48
|
1 |
|
public function send($notifiable, Notification $notification) |
49
|
|
|
{ |
50
|
1 |
|
$tokens = (array) $notifiable->routeNotificationFor('gcm', $notification); |
51
|
1 |
|
if (empty($tokens)) { |
52
|
1 |
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$message = $notification->toGcm($notifiable); |
56
|
|
|
if (! $message) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$packet = $this->getPacket($tokens, $message); |
61
|
|
|
|
62
|
|
|
try { |
63
|
|
|
$response = $this->client->send($packet); |
64
|
|
|
} catch (Exception $exception) { |
65
|
|
|
throw SendingFailed::create($exception); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (! $response->getFailureCount() == 0) { |
69
|
|
|
$this->handleFailedNotifications($notifiable, $notification, $response); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param $tokens |
75
|
|
|
* @param $message |
76
|
|
|
* @return \NotificationChannels\Gcm\Packet |
77
|
|
|
*/ |
78
|
|
|
protected function getPacket($tokens, $message) |
79
|
|
|
{ |
80
|
|
|
$packet = new Packet(); |
81
|
|
|
|
82
|
|
|
$packet->setRegistrationIds($tokens); |
83
|
|
|
$packet->setCollapseKey(str_slug($message->title)); |
|
|
|
|
84
|
|
|
$packet->setData([ |
85
|
|
|
'title' => $message->title, |
86
|
|
|
'message' => $message->message, |
87
|
|
|
] + $message->data); |
88
|
|
|
$packet->setNotification([ |
89
|
|
|
'title' => $message->title, |
90
|
|
|
'body' => $message->message, |
91
|
|
|
'sound' => $message->sound, |
92
|
|
|
] + $message->notification); |
93
|
|
|
|
94
|
|
|
return $packet; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Handle a failed notification. |
99
|
|
|
* |
100
|
|
|
* @param mixed $notifiable |
101
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
102
|
|
|
* @param $response |
103
|
|
|
*/ |
104
|
|
|
protected function handleFailedNotifications($notifiable, Notification $notification, $response) |
105
|
|
|
{ |
106
|
|
|
$results = $response->getResults(); |
107
|
|
|
|
108
|
|
|
foreach ($results as $token => $result) { |
109
|
|
|
if (! isset($result['error'])) { |
110
|
|
|
continue; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->events->dispatch( |
114
|
|
|
new NotificationFailed($notifiable, $notification, get_class($this), [ |
115
|
|
|
'token' => $token, |
116
|
|
|
'error' => $result['error'], |
117
|
|
|
]) |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
This function has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.