-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathMessage.php
More file actions
243 lines (204 loc) · 6.8 KB
/
Message.php
File metadata and controls
243 lines (204 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
namespace sngrl\PhpFirebaseCloudMessaging;
use sngrl\PhpFirebaseCloudMessaging\Recipient\Recipient;
use sngrl\PhpFirebaseCloudMessaging\Recipient\Topic;
use sngrl\PhpFirebaseCloudMessaging\Recipient\Device;
/**
* @author sngrl
*/
class Message implements \JsonSerializable
{
/**
* Maximum topics and devices: https://firebase.google.com/docs/cloud-messaging/http-server-ref#send-downstream
*/
const MAX_TOPICS = 3;
const MAX_DEVICES = 1000;
private $notification;
private $collapseKey;
private $priority;
private $data;
private $recipients = [];
private $recipientType;
private $jsonData;
private $condition;
public function __construct() {
$this->jsonData = [];
}
/**
* where should the message go
*
* @param Recipient $recipient
*
* @return \sngrl\PhpFirebaseCloudMessaging\Message
*/
public function addRecipient(Recipient $recipient)
{
$this->recipients[] = $recipient;
if (!isset($this->recipientType)) {
$this->recipientType = get_class($recipient);
}
if ($this->recipientType !== get_class($recipient)) {
throw new \InvalidArgumentException('mixed recepient types are not supported by FCM');
}
return $this;
}
public function setNotification(Notification $notification)
{
$this->notification = $notification;
return $this;
}
public function setCollapseKey($collapseKey)
{
$this->collapseKey = $collapseKey;
return $this;
}
public function setPriority($priority)
{
$this->priority = $priority;
return $this;
}
public function setData(array $data)
{
$this->data = $data;
return $this;
}
/**
* Specify a condition pattern when sending to combinations of topics
* https://firebase.google.com/docs/cloud-messaging/topic-messaging#sending_topic_messages_from_the_server
*
* Examples:
* "%s && %s" > Send to devices subscribed to topic 1 and topic 2
* "%s && (%s || %s)" > Send to devices subscribed to topic 1 and topic 2 or 3
*
* @param string $condition
* @return $this
*/
public function setCondition($condition) {
$this->condition = $condition;
return $this;
}
/**
* Set root message data via key
*
* @param string $key
* @param mixed $value
* @return $this
*/
public function setJsonKey($key, $value) {
$this->jsonData[$key] = $value;
return $this;
}
/**
* Unset root message data via key
*
* @param string $key
* @return $this
*/
public function unsetJsonKey($key) {
unset($this->jsonData[$key]);
return $this;
}
/**
* Get root message data via key
*
* @param string $key
* @return mixed
*/
public function getJsonKey($key) {
return $this->jsonData[$key];
}
/**
* Get root message data
*
* @return array
*/
public function getJsonData() {
return $this->jsonData;
}
/**
* Set root message data
*
* @param array $array
* @return $this
*/
public function setJsonData($array) {
$this->jsonData = $array;
return $this;
}
public function setDelayWhileIdle($value)
{
$this->setJsonKey('delay_while_idle', (bool)$value);
return $this;
}
public function setTimeToLive($value)
{
$this->setJsonKey('time_to_live', (int)$value);
return $this;
}
public function jsonSerialize()
{
$jsonData = $this->jsonData;
if (empty($this->recipients)) {
throw new \UnexpectedValueException('Message must have at least one recipient');
}
if (count($this->recipients) == 1) {
$jsonData['to'] = $this->createTarget();
} elseif ($this->recipientType == Device::class) {
$jsonData['registration_ids'] = $this->createTarget();
} else {
$jsonData['condition'] = $this->createTarget();
}
if ($this->collapseKey) {
$jsonData['collapse_key'] = $this->collapseKey;
}
if ($this->data) {
$jsonData['data'] = $this->data;
}
if ($this->priority) {
$jsonData['priority'] = $this->priority;
}
if ($this->notification) {
$jsonData['notification'] = $this->notification;
}
return $jsonData;
}
private function createTarget()
{
$recipientCount = count($this->recipients);
switch ($this->recipientType) {
case Topic::class:
if ($recipientCount == 1) {
return sprintf('/topics/%s', current($this->recipients)->getName());
} else if ($recipientCount > self::MAX_TOPICS) {
throw new \OutOfRangeException(sprintf('Message topic limit exceeded. Firebase supports a maximum of %u topics.', self::MAX_TOPICS));
} else if (!$this->condition) {
throw new \InvalidArgumentException('Missing message condition. You must specify a condition pattern when sending to combinations of topics.');
} else if ($recipientCount != substr_count($this->condition, '%s')) {
throw new \UnexpectedValueException('The number of message topics must match the number of occurrences of "%s" in the condition pattern.');
} else {
$names = [];
foreach ($this->recipients as $recipient) {
$names[] = vsprintf("'%s' in topics", [$recipient->getName()]);
}
return vsprintf($this->condition, $names);
}
break;
case Device::class:
if ($recipientCount == 1) {
return current($this->recipients)->getToken();
} else if ($recipientCount > self::MAX_DEVICES) {
throw new \OutOfRangeException(sprintf('Message device limit exceeded. Firebase supports a maximum of %u devices.', self::MAX_DEVICES));
} else {
$ids = [];
foreach ($this->recipients as $recipient) {
$ids[] = $recipient->getToken();
}
return $ids;
}
break;
default:
break;
}
return null;
}
}