1
|
|
|
<?php |
2
|
|
|
namespace nstdio\notymo; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class GCMNotificationComponent |
6
|
|
|
*/ |
7
|
|
|
class GCMNotification extends AbstractNotification |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
const GCM_HOST = 'https://gcm-http.googleapis.com/gcm/send'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
const MAX_TOKENS = 1000; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $headers; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* GCMNotificationComponent constructor. |
26
|
|
|
* |
27
|
|
|
* @param $apiKey |
28
|
|
|
*/ |
29
|
2 |
|
public function __construct($apiKey) |
30
|
|
|
{ |
31
|
2 |
|
parent::__construct(); |
32
|
|
|
|
33
|
2 |
|
$this->headers = array( |
34
|
2 |
|
'Authorization: key=' . $apiKey, |
35
|
2 |
|
'Content-Type: application/json', |
36
|
|
|
); |
37
|
2 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* |
41
|
|
|
*/ |
42
|
2 |
|
public function send() |
43
|
|
|
{ |
44
|
2 |
|
if ($this->messageQueue->isEmpty()) { |
45
|
1 |
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
$this->openConnection(); |
49
|
|
|
|
50
|
|
|
/** @var MessageInterface $message */ |
51
|
1 |
|
foreach ($this->messageQueue as $message) { |
52
|
1 |
|
$this->sendMessage($message); |
53
|
1 |
|
} |
54
|
|
|
|
55
|
1 |
|
$this->stream->close(); |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $message |
60
|
|
|
*/ |
61
|
1 |
|
private function sendMessage($message) |
62
|
|
|
{ |
63
|
1 |
|
$data = $this->createPayload($message); |
64
|
|
|
|
65
|
1 |
|
$this->stream->write(CURLOPT_POSTFIELDS, json_encode($data)); |
66
|
|
|
|
67
|
1 |
|
return $this->stream->read(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param MessageInterface $message |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
1 |
|
final protected function createPayload(MessageInterface $message) |
76
|
|
|
{ |
77
|
|
|
$data = array( |
78
|
1 |
|
'registration_ids' => $this->getTokens($message), |
79
|
|
|
'data' => array( |
80
|
1 |
|
'message' => $message->getMessage(), |
81
|
1 |
|
'sound' => $message->getSound(), |
82
|
1 |
|
'badge' => $message->getBadge(), |
83
|
1 |
|
), |
84
|
1 |
|
); |
85
|
1 |
|
$customData = $message->getCustomData(); |
86
|
1 |
|
if (!empty($customData)) { |
87
|
1 |
|
$data['data'] = array_merge($data['data'], $customData); |
88
|
1 |
|
} |
89
|
|
|
|
90
|
1 |
|
return $data; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
private function getTokens(MessageInterface $message) |
94
|
|
|
{ |
95
|
1 |
|
$ret = array(); |
96
|
1 |
|
if ($message->isMultiple()) { |
97
|
1 |
|
foreach ($message->getToken() as $token) { |
98
|
1 |
|
$ret[] = $token; |
99
|
1 |
|
} |
100
|
1 |
|
} else { |
101
|
1 |
|
$ret[] = $message->getToken(); |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
return $ret; |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
public function enqueue(MessageInterface $message) |
108
|
|
|
{ |
109
|
1 |
|
if ($message->isMultiple() && count($message->getToken()) > self::MAX_TOKENS) { |
110
|
1 |
|
$parts = array_chunk($message->getToken(), self::MAX_TOKENS); |
111
|
1 |
|
$message->setToken(null); |
112
|
1 |
|
$firstElement = array_shift($parts); |
113
|
|
|
|
114
|
1 |
|
foreach ($parts as $item) { |
115
|
1 |
|
$clonedMessage = clone $message; |
116
|
1 |
|
$clonedMessage->setToken($item); |
117
|
1 |
|
parent::enqueue($clonedMessage); |
118
|
1 |
|
} |
119
|
|
|
|
120
|
1 |
|
$message->setToken($firstElement); |
121
|
1 |
|
} |
122
|
|
|
|
123
|
1 |
|
parent::enqueue($message); |
124
|
1 |
|
} |
125
|
|
|
|
126
|
1 |
|
protected function getConnectionParams() |
127
|
|
|
{ |
128
|
|
|
return array( |
129
|
1 |
|
CURLOPT_URL => self::GCM_HOST, |
130
|
1 |
|
CURLOPT_POST => true, |
131
|
1 |
|
CURLOPT_HTTPHEADER => $this->headers, |
132
|
1 |
|
CURLOPT_RETURNTRANSFER => true, |
133
|
1 |
|
); |
134
|
|
|
} |
135
|
|
|
} |