1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NotificationChannels\Fcm; |
4
|
|
|
|
5
|
|
|
class FcmMessage |
6
|
|
|
{ |
7
|
|
|
const PRIORITY_NORMAL = 'normal'; |
8
|
|
|
|
9
|
|
|
const PRIORITY_HIGH = 'high'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $fcmKey; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $to; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $registrationIds; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $condition; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $collapseKey; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
protected $contentAvailable; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
protected $mutableContent; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $priority; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var int |
53
|
|
|
*/ |
54
|
|
|
protected $timeToLive = 2419200; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var bool |
58
|
|
|
*/ |
59
|
|
|
protected $dryRun = false; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var array|object |
63
|
|
|
*/ |
64
|
|
|
protected $data; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var FcmNotification |
68
|
|
|
*/ |
69
|
|
|
protected $notification; |
70
|
|
|
|
71
|
|
|
public static function create() |
72
|
|
|
{ |
73
|
|
|
return new static(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getFcmKey() |
80
|
|
|
{ |
81
|
|
|
return $this->fcmKey; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* This method can be used to override the default FCM key used in the config/broadcasting.php. |
86
|
|
|
* |
87
|
|
|
* @param string $fcmKey |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function setFcmKey($fcmKey) |
91
|
|
|
{ |
92
|
|
|
$this->fcmKey = $fcmKey; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getTo() |
101
|
|
|
{ |
102
|
|
|
return $this->to; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* This parameter specifies the recipient of a message. |
107
|
|
|
* The value can be a device's registration token, a device group's notification key, or a single topic (prefixed with /topics/). To send to multiple |
108
|
|
|
* topics, use the condition parameter. |
109
|
|
|
* |
110
|
|
|
* @param string $to |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function setTo($to) |
114
|
|
|
{ |
115
|
|
|
$this->to = $to; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function getRegistrationIds() |
124
|
|
|
{ |
125
|
|
|
return $this->registrationIds; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* This parameter specifies the recipient of a multicast message, a message sent to more than one registration token. |
130
|
|
|
* The value should be an array of registration tokens to which to send the multicast message. The array must contain at least 1 and at most 1000 |
131
|
|
|
* registration tokens. To send a message to a single device, use the to parameter. |
132
|
|
|
* Multicast messages are only allowed using the HTTP JSON format. |
133
|
|
|
* |
134
|
|
|
* @param array $registrationIds |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
|
|
public function setRegistrationIds($registrationIds) |
138
|
|
|
{ |
139
|
|
|
$this->registrationIds = $registrationIds; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function getCondition() |
148
|
|
|
{ |
149
|
|
|
return $this->condition; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* [Optional] This parameter specifies a logical expression of conditions that determine the message target. |
154
|
|
|
* Supported condition: Topic, formatted as "'yourTopic' in topics". This value is case-insensitive. |
155
|
|
|
* Supported operators: &&, ||. Maximum two operators per topic message supported. |
156
|
|
|
* |
157
|
|
|
* @param string $condition |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
|
|
public function setCondition($condition) |
161
|
|
|
{ |
162
|
|
|
$this->condition = $condition; |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
public function getCollapseKey() |
171
|
|
|
{ |
172
|
|
|
return $this->collapseKey; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* [Optional] This parameter identifies a group of messages (e.g., with collapse_key: "Updates Available") that can be collapsed, so that only the last |
177
|
|
|
* message gets sent when delivery can be resumed. This is intended to avoid sending too many of the same messages when the device comes back online or |
178
|
|
|
* becomes active. |
179
|
|
|
* Note that there is no guarantee of the order in which messages get sent. |
180
|
|
|
* Note: A maximum of 4 different collapse keys is allowed at any given time. This means a FCM connection server can simultaneously store 4 different |
181
|
|
|
* send-to-sync messages per client app. If you exceed this number, there is no guarantee which 4 collapse keys the FCM connection server will keep. |
182
|
|
|
* |
183
|
|
|
* @param string $collapseKey |
184
|
|
|
* @return $this |
185
|
|
|
*/ |
186
|
|
|
public function setCollapseKey($collapseKey) |
187
|
|
|
{ |
188
|
|
|
$this->collapseKey = $collapseKey; |
189
|
|
|
|
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return bool |
195
|
|
|
*/ |
196
|
|
|
public function isContentAvailable() |
197
|
|
|
{ |
198
|
|
|
return $this->contentAvailable; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* [Optional] On iOS, use this field to represent content-available in the APNs payload. When a notification or message is sent and this is set to true, an |
203
|
|
|
* inactive client app is awoken. On Android, data messages wake the app by default. On Chrome, currently not supported. |
204
|
|
|
* |
205
|
|
|
* @param bool $contentAvailable |
206
|
|
|
* @return $this |
207
|
|
|
*/ |
208
|
|
|
public function setContentAvailable($contentAvailable) |
209
|
|
|
{ |
210
|
|
|
$this->contentAvailable = $contentAvailable; |
211
|
|
|
|
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return bool |
217
|
|
|
*/ |
218
|
|
|
public function isMutableContent() |
219
|
|
|
{ |
220
|
|
|
return $this->mutableContent; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* [Optional] Currently for iOS 10+ devices only. On iOS, use this field to represent mutable-content in the APNS payload. When a notification is sent and |
225
|
|
|
* this is set to true, the content of the notification can be modified before it is displayed, using a Notification Service app extension. This parameter |
226
|
|
|
* will be ignored for Android and web. |
227
|
|
|
* |
228
|
|
|
* @param bool $mutableContent |
229
|
|
|
* @return $this |
230
|
|
|
*/ |
231
|
|
|
public function setMutableContent($mutableContent) |
232
|
|
|
{ |
233
|
|
|
$this->mutableContent = $mutableContent; |
234
|
|
|
|
235
|
|
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
|
|
public function getPriority() |
242
|
|
|
{ |
243
|
|
|
return $this->priority; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* [Optional] Sets the priority of the message. Valid values are "normal" and "high." On iOS, these correspond to APNs priorities 5 and 10. |
248
|
|
|
* By default, notification messages are sent with high priority, and data messages are sent with normal priority. Normal priority optimizes the client |
249
|
|
|
* app's battery consumption and should be used unless immediate delivery is required. For messages with normal priority, the app may receive the message |
250
|
|
|
* with unspecified delay. |
251
|
|
|
* When a message is sent with high priority, it is sent immediately, and the app can wake a sleeping device and open a network connection to your server. |
252
|
|
|
* |
253
|
|
|
* @param string $priority |
254
|
|
|
* @return $this |
255
|
|
|
*/ |
256
|
|
|
public function setPriority($priority) |
257
|
|
|
{ |
258
|
|
|
$this->priority = $priority; |
259
|
|
|
|
260
|
|
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @return int |
265
|
|
|
*/ |
266
|
|
|
public function getTimeToLive() |
267
|
|
|
{ |
268
|
|
|
return $this->timeToLive; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* This parameter specifies how long (in seconds) the message should be kept in FCM storage if the device is offline. The maximum time to live |
273
|
|
|
* supported is 4 weeks, and the default value is 4 weeks. |
274
|
|
|
* |
275
|
|
|
* @param int $timeToLive |
276
|
|
|
* @return $this |
277
|
|
|
*/ |
278
|
|
|
public function setTimeToLive($timeToLive) |
279
|
|
|
{ |
280
|
|
|
$this->timeToLive = $timeToLive; |
281
|
|
|
|
282
|
|
|
return $this; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @return bool |
287
|
|
|
*/ |
288
|
|
|
public function isDryRun() |
289
|
|
|
{ |
290
|
|
|
return $this->dryRun; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* This parameter, when set to true, allows developers to test a request without actually sending a message. |
295
|
|
|
* |
296
|
|
|
* @param bool $dryRun |
297
|
|
|
* @return $this |
298
|
|
|
*/ |
299
|
|
|
public function setDryRun($dryRun) |
300
|
|
|
{ |
301
|
|
|
$this->dryRun = $dryRun; |
302
|
|
|
|
303
|
|
|
return $this; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @return array|object |
308
|
|
|
*/ |
309
|
|
|
public function getData() |
310
|
|
|
{ |
311
|
|
|
return $this->data; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* [Optional] This parameter specifies the custom key-value pairs of the message's payload. |
316
|
|
|
* For example, with data:{"score":"3x1"}: |
317
|
|
|
* On iOS, if the message is sent via APNS, it represents the custom data fields. If it is sent via FCM connection server, it would be represented as key |
318
|
|
|
* value dictionary in AppDelegate application:didReceiveRemoteNotification:. |
319
|
|
|
* On Android, this would result in an intent extra named score with the string value 3x1. |
320
|
|
|
* The key should not be a reserved word ("from" or any word starting with "google" or "gcm"). Do not use any of the words defined in this table (such as |
321
|
|
|
* collapse_key). |
322
|
|
|
* Values in string types are recommended. You have to convert values in objects or other non-string data types (e.g., integers or booleans) to string. |
323
|
|
|
* |
324
|
|
|
* @param array|object $data |
325
|
|
|
* @return $this |
326
|
|
|
*/ |
327
|
|
|
public function setData($data) |
328
|
|
|
{ |
329
|
|
|
$this->data = $data; |
330
|
|
|
|
331
|
|
|
return $this; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* @return FcmNotification |
336
|
|
|
*/ |
337
|
|
|
public function getNotification() |
338
|
|
|
{ |
339
|
|
|
return $this->notification; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* [Optional] This parameter specifies the predefined, user-visible key-value pairs of the notification payload. |
344
|
|
|
* |
345
|
|
|
* @param FcmNotification $notification |
346
|
|
|
* @return $this |
347
|
|
|
*/ |
348
|
|
|
public function setNotification($notification) |
349
|
|
|
{ |
350
|
|
|
$this->notification = $notification; |
351
|
|
|
|
352
|
|
|
return $this; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @return string |
357
|
|
|
*/ |
358
|
|
|
public function toJson() |
359
|
|
|
{ |
360
|
|
|
return json_encode($this->toArray()); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* @return array |
365
|
|
|
*/ |
366
|
|
|
public function toArray() |
367
|
|
|
{ |
368
|
|
|
$array = [ |
369
|
|
|
'to' => $this->to, |
370
|
|
|
'registration_ids' => $this->registrationIds, |
371
|
|
|
'condition' => $this->condition, |
372
|
|
|
'collapse_key' => $this->collapseKey, |
373
|
|
|
'content_available' => $this->contentAvailable, |
374
|
|
|
'mutable_content' => $this->mutableContent, |
375
|
|
|
'priority' => $this->priority, |
376
|
|
|
'time_to_live' => $this->timeToLive, |
377
|
|
|
'dry_run' => $this->dryRun, |
378
|
|
|
'data' => $this->data, |
379
|
|
|
]; |
380
|
|
|
|
381
|
|
|
if ($this->notification) { |
382
|
|
|
$array['notification'] = $this->notification->toArray(); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
return $array; |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
|