1
1
<?php
2
2
namespace Plokko \LaravelFirebase ;
3
3
4
-
4
+ use JsonSerializable ;
5
5
use Plokko \Firebase \ServiceAccount ;
6
6
use Plokko \Firebase \FCM \{
7
- Exceptions \UnregisteredException , Message , Request , Targets \Condition , Targets \Target , Targets \Token , Targets \Topic
7
+ Exceptions \UnregisteredException ,
8
+ Message ,
9
+ Request ,
10
+ Targets \Condition ,
11
+ Targets \Target ,
12
+ Targets \Token ,
13
+ Targets \Topic
8
14
};
9
15
use Plokko \LaravelFirebase \Exceptions \FcmTargetNotSpecifiedException ;
16
+ use Illuminate \Contracts \Support \Arrayable ;
10
17
11
- class FcmMessageBuilder
18
+ class FcmMessageBuilder implements JsonSerializable, Arrayable
12
19
{
13
20
private
14
- /**
15
- * @var ServiceAccount
16
- */
17
- $ serviceAccount ,
18
- /**
19
- * @var Message
20
- */
21
- $ message ,
22
- /**
23
- * @var string|null
24
- */
25
- $ invalidTokenEvent ;
21
+ /**
22
+ * @var ServiceAccount
23
+ */
24
+ $ serviceAccount ,
25
+ /**
26
+ * @var Message
27
+ */
28
+ $ message ,
29
+ /**
30
+ * @var string|null
31
+ */
32
+ $ invalidTokenEvent ;
26
33
27
34
function __construct (ServiceAccount $ serviceAccount )
28
35
{
@@ -35,13 +42,15 @@ function __construct(ServiceAccount $serviceAccount)
35
42
* @param string $eventName
36
43
* @return $this
37
44
*/
38
- function setInvalidTokenEvent ($ eventName ){
45
+ function setInvalidTokenEvent ($ eventName )
46
+ {
39
47
$ this ->invalidTokenEvent = $ eventName ;
40
48
return $ this ;
41
49
}
42
50
43
51
44
- function __get ($ k ){
52
+ function __get ($ k )
53
+ {
45
54
return $ this ->message ->{$ k };
46
55
}
47
56
@@ -51,7 +60,8 @@ function __get($k){
51
60
* @param array $data
52
61
* @return $this
53
62
*/
54
- function data (array $ data ){
63
+ function data (array $ data )
64
+ {
55
65
$ this ->message ->data ->fill ($ data );
56
66
return $ this ;
57
67
}
@@ -61,19 +71,25 @@ function data(array $data){
61
71
* @param string $title
62
72
* @return $this
63
73
*/
64
- function notificationTitle ($ title ){
74
+ function notificationTitle ($ title )
75
+ {
65
76
$ this ->message ->notification ->setTitle ($ title );
66
77
return $ this ;
67
78
}
68
79
69
80
/**
70
81
* Set the notification sound (Android/IOS);
71
82
* set to "default" for default sound, null to remove
72
- * @param string $sound
83
+ * @param string $sound Sound name (use 'default' for default sound)
84
+ * @param bool|string $applyApns Specify also in apns payload data for IOS, if true is used $sound will be used, with false apns will not be set otherwise the specified value will be used.
73
85
* @return $this
74
86
*/
75
- function notificationSound ($ sound ){
87
+ function notificationSound ($ sound , $ apns = true )
88
+ {
76
89
$ this ->message ->android ->notification ->sound = $ sound ;
90
+ if ($ apns !== false ) {
91
+ $ this ->setApnsApsData ('sound ' , $ apns === true ? $ sound : $ apns );
92
+ }
77
93
return $ this ;
78
94
}
79
95
@@ -82,7 +98,8 @@ function notificationSound($sound){
82
98
* @param string $body
83
99
* @return $this
84
100
*/
85
- function notificationBody ($ body ){
101
+ function notificationBody ($ body )
102
+ {
86
103
$ this ->message ->notification ->setBody ($ body );
87
104
return $ this ;
88
105
}
@@ -92,8 +109,9 @@ function notificationBody($body){
92
109
* @param 'high'|"normal" $priority
93
110
* @return $this
94
111
*/
95
- function priority ($ priority ){
96
- if ($ priority !=='high ' && $ priority !=='normal ' )
112
+ function priority ($ priority )
113
+ {
114
+ if ($ priority !== 'high ' && $ priority !== 'normal ' )
97
115
throw new \InvalidArgumentException ('Invalid priority value! ' );
98
116
$ this ->message ->android ->setPriority ($ priority );
99
117
return $ this ;
@@ -103,7 +121,8 @@ function priority($priority){
103
121
* @param string $tag
104
122
* @return $this
105
123
*/
106
- function notificationTag ($ tag ){
124
+ function notificationTag ($ tag )
125
+ {
107
126
$ this ->message ->android ->notification ->tag = $ tag ;
108
127
return $ this ;
109
128
}
@@ -114,7 +133,8 @@ function notificationTag($tag){
114
133
* @param Closure(\Plokko\Firebase\FCM\Message\AndroidNotification) $closure
115
134
* @return $this
116
135
*/
117
- function setAndroidNotification ($ closure ){
136
+ function setAndroidNotification ($ closure )
137
+ {
118
138
$ closure ($ this ->android ->notification );
119
139
return $ this ;
120
140
}
@@ -124,7 +144,8 @@ function setAndroidNotification($closure){
124
144
* @param Closure(\Plokko\Firebase\FCM\Message\AndroidConfig) $closure
125
145
* @return $this
126
146
*/
127
- function setAndroidConfig ($ closure ){
147
+ function setAndroidConfig ($ closure )
148
+ {
128
149
$ closure ($ this ->android );
129
150
return $ this ;
130
151
}
@@ -133,7 +154,8 @@ function setAndroidConfig($closure){
133
154
* Set high priority
134
155
* @return $this
135
156
*/
136
- function highPriority (){
157
+ function highPriority ()
158
+ {
137
159
$ this ->priority ('high ' );
138
160
return $ this ;
139
161
}
@@ -142,7 +164,8 @@ function highPriority(){
142
164
* Set normal priority
143
165
* @return $this
144
166
*/
145
- function normalPriority (){
167
+ function normalPriority ()
168
+ {
146
169
$ this ->priority ('normal ' );
147
170
return $ this ;
148
171
}
@@ -152,19 +175,76 @@ function normalPriority(){
152
175
* @param string $ttl TTL as a string (ex. '14.5s')
153
176
* @return $this
154
177
*/
155
- function ttl ($ ttl ){
178
+ function ttl ($ ttl )
179
+ {
156
180
$ this ->message ->android ->ttl ($ ttl );
157
181
return $ this ;
158
182
}
159
183
184
+ /**
185
+ * Set Aps data in the Apns payload
186
+ * @param string $key Key to set
187
+ * @param mixed $value Value to set
188
+ * @return $this
189
+ */
190
+ function setApnsApsData ($ key , $ value )
191
+ {
192
+ $ this ->message ->apns ->payload ->aps ->$ key = $ value ;
193
+ return $ this ;
194
+ }
195
+
196
+ /**
197
+ * Set Apns payload data
198
+ * @param string $key Key of the payload to set
199
+ * @param mixed $value Value of the payload to set
200
+ * @return $this
201
+ */
202
+ function setApnsPayload ($ key , $ value )
203
+ {
204
+ $ this ->message ->apns ->payload ->$ key = $ value ;
205
+ return $ this ;
206
+ }
207
+
208
+ /**
209
+ * Get Apns payload data
210
+ * @param string $key Key of the payload to get
211
+ * @return mixed
212
+ */
213
+ function getApnsPayloadValue ($ key )
214
+ {
215
+ return $ this ->message ->apns ->payload ->$ key ;
216
+ }
217
+
218
+ /**
219
+ * Set Apns header data
220
+ * @param string $key Key of the header to set
221
+ * @param mixed $value Value of the header to set
222
+ * @return $this
223
+ */
224
+ function setApnsHeader ($ key , $ value )
225
+ {
226
+ $ this ->message ->apns ->header ->$ key = $ value ;
227
+ return $ this ;
228
+ }
229
+
230
+ /**
231
+ * Get Apns header data
232
+ * @param string $key Key of the header to get
233
+ * @return mixed
234
+ */
235
+ function getApnsHeaderValue ($ key )
236
+ {
237
+ return $ this ->message ->header ->$ key ;
238
+ }
160
239
161
240
/**
162
241
* Set the message destination,
163
242
* this field is MANDATORY to submit the message
164
243
* @param Target $target
165
244
* @return FcmMessageBuilder
166
245
*/
167
- function toTarget (Target $ target ){
246
+ function toTarget (Target $ target )
247
+ {
168
248
$ this ->message ->setTarget ($ target );
169
249
return $ this ;
170
250
}
@@ -174,7 +254,8 @@ function toTarget(Target $target){
174
254
* @param string $token Target device FCM token
175
255
* @return FcmMessageBuilder
176
256
*/
177
- function toDevice ($ token ){
257
+ function toDevice ($ token )
258
+ {
178
259
return $ this ->toTarget (new Token ($ token ));
179
260
}
180
261
@@ -183,7 +264,8 @@ function toDevice($token){
183
264
* @param string $topicName Target topic name
184
265
* @return FcmMessageBuilder
185
266
*/
186
- function toTopic ($ topicName ){
267
+ function toTopic ($ topicName )
268
+ {
187
269
return $ this ->toTarget (new Topic ($ topicName ));
188
270
}
189
271
@@ -192,7 +274,8 @@ function toTopic($topicName){
192
274
* @param string $condition Target condition
193
275
* @return FcmMessageBuilder
194
276
*/
195
- function toCondition ($ condition ){
277
+ function toCondition ($ condition )
278
+ {
196
279
return $ this ->toTarget (new Condition ($ condition ));
197
280
}
198
281
@@ -201,7 +284,8 @@ function toCondition($condition){
201
284
* @internal
202
285
* @return Request
203
286
*/
204
- private function request (){
287
+ private function request ()
288
+ {
205
289
return new Request ($ this ->serviceAccount );
206
290
}
207
291
@@ -212,20 +296,87 @@ private function request(){
212
296
* @throws \Plokko\Firebase\FCM\Exceptions\FcmErrorException FCMError exception
213
297
* @throws FcmTargetNotSpecifiedException will be thrown if no device target is specified
214
298
*/
215
- function send (){
299
+ function send ()
300
+ {
216
301
217
- if ($ this ->message ->target === null ){
302
+ if ($ this ->message ->target === null ) {
218
303
throw new FcmTargetNotSpecifiedException ();
219
304
}
220
305
try {
221
306
$ this ->message ->send ($ this ->request ());
222
- }catch (UnregisteredException $ e ){
223
- if ($ this ->invalidTokenEvent ) {
224
- event ($ this ->invalidTokenEvent ,$ this ->message ->target );
307
+ } catch (UnregisteredException $ e ) {
308
+ if ($ this ->invalidTokenEvent ) {
309
+ event ($ this ->invalidTokenEvent , $ this ->message ->target );
310
+ }
311
+ throw $ e ;
312
+ }
313
+
314
+ }
315
+
316
+
317
+ /**
318
+ * Validate the message with Firebase without submitting it
319
+ * @throws \GuzzleHttp\Exception\GuzzleException Generic http exception
320
+ * @throws \Plokko\Firebase\FCM\Exceptions\FcmErrorException FCMError exception
321
+ * @throws FcmTargetNotSpecifiedException will be thrown if no device target is specified
322
+ */
323
+ function validate ()
324
+ {
325
+ if ($ this ->message ->target === null ) {
326
+ throw new FcmTargetNotSpecifiedException ();
327
+ }
328
+ try {
329
+ $ this ->message ->validate ($ this ->request ());
330
+ } catch (UnregisteredException $ e ) {
331
+ if ($ this ->invalidTokenEvent ) {
332
+ event ($ this ->invalidTokenEvent , $ this ->message ->target );
225
333
}
226
334
throw $ e ;
227
335
}
228
336
229
337
}
230
338
231
- }
339
+ /**
340
+ * Get FCM message class
341
+ * @return Message
342
+ */
343
+ public function getMessage ()
344
+ {
345
+ return $ this ->message ;
346
+ }
347
+
348
+ /**
349
+ * Get FCM message payload
350
+ * @return array
351
+ */
352
+ public function getPayload ()
353
+ {
354
+ return $ this ->message ->getPayload ();
355
+ }
356
+
357
+ /**
358
+ * Cast to array
359
+ * @return array
360
+ */
361
+ public function toArray ()
362
+ {
363
+ return $ this ->getPayload ();
364
+ }
365
+
366
+ /**
367
+ * Cast for JSON serialization
368
+ * @return array
369
+ */
370
+ public function jsonSerialize (): mixed
371
+ {
372
+ return $ this ->toArray ();
373
+ }
374
+ /**
375
+ * Cast to string (as JSON string)
376
+ * @return string
377
+ */
378
+ function __toString ()
379
+ {
380
+ return json_encode ($ this );
381
+ }
382
+ }
0 commit comments