11<?php
22namespace Plokko \LaravelFirebase ;
33
4-
4+ use JsonSerializable ;
55use Plokko \Firebase \ServiceAccount ;
66use 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
814};
915use Plokko \LaravelFirebase \Exceptions \FcmTargetNotSpecifiedException ;
16+ use Illuminate \Contracts \Support \Arrayable ;
1017
11- class FcmMessageBuilder
18+ class FcmMessageBuilder implements JsonSerializable, Arrayable
1219{
1320 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 ;
2633
2734 function __construct (ServiceAccount $ serviceAccount )
2835 {
@@ -35,13 +42,15 @@ function __construct(ServiceAccount $serviceAccount)
3542 * @param string $eventName
3643 * @return $this
3744 */
38- function setInvalidTokenEvent ($ eventName ){
45+ function setInvalidTokenEvent ($ eventName )
46+ {
3947 $ this ->invalidTokenEvent = $ eventName ;
4048 return $ this ;
4149 }
4250
4351
44- function __get ($ k ){
52+ function __get ($ k )
53+ {
4554 return $ this ->message ->{$ k };
4655 }
4756
@@ -51,7 +60,8 @@ function __get($k){
5160 * @param array $data
5261 * @return $this
5362 */
54- function data (array $ data ){
63+ function data (array $ data )
64+ {
5565 $ this ->message ->data ->fill ($ data );
5666 return $ this ;
5767 }
@@ -61,19 +71,25 @@ function data(array $data){
6171 * @param string $title
6272 * @return $this
6373 */
64- function notificationTitle ($ title ){
74+ function notificationTitle ($ title )
75+ {
6576 $ this ->message ->notification ->setTitle ($ title );
6677 return $ this ;
6778 }
6879
6980 /**
7081 * Set the notification sound (Android/IOS);
7182 * 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.
7385 * @return $this
7486 */
75- function notificationSound ($ sound ){
87+ function notificationSound ($ sound , $ apns = true )
88+ {
7689 $ this ->message ->android ->notification ->sound = $ sound ;
90+ if ($ apns !== false ) {
91+ $ this ->setApnsApsData ('sound ' , $ apns === true ? $ sound : $ apns );
92+ }
7793 return $ this ;
7894 }
7995
@@ -82,7 +98,8 @@ function notificationSound($sound){
8298 * @param string $body
8399 * @return $this
84100 */
85- function notificationBody ($ body ){
101+ function notificationBody ($ body )
102+ {
86103 $ this ->message ->notification ->setBody ($ body );
87104 return $ this ;
88105 }
@@ -92,8 +109,9 @@ function notificationBody($body){
92109 * @param 'high'|"normal" $priority
93110 * @return $this
94111 */
95- function priority ($ priority ){
96- if ($ priority !=='high ' && $ priority !=='normal ' )
112+ function priority ($ priority )
113+ {
114+ if ($ priority !== 'high ' && $ priority !== 'normal ' )
97115 throw new \InvalidArgumentException ('Invalid priority value! ' );
98116 $ this ->message ->android ->setPriority ($ priority );
99117 return $ this ;
@@ -103,7 +121,8 @@ function priority($priority){
103121 * @param string $tag
104122 * @return $this
105123 */
106- function notificationTag ($ tag ){
124+ function notificationTag ($ tag )
125+ {
107126 $ this ->message ->android ->notification ->tag = $ tag ;
108127 return $ this ;
109128 }
@@ -114,7 +133,8 @@ function notificationTag($tag){
114133 * @param Closure(\Plokko\Firebase\FCM\Message\AndroidNotification) $closure
115134 * @return $this
116135 */
117- function setAndroidNotification ($ closure ){
136+ function setAndroidNotification ($ closure )
137+ {
118138 $ closure ($ this ->android ->notification );
119139 return $ this ;
120140 }
@@ -124,7 +144,8 @@ function setAndroidNotification($closure){
124144 * @param Closure(\Plokko\Firebase\FCM\Message\AndroidConfig) $closure
125145 * @return $this
126146 */
127- function setAndroidConfig ($ closure ){
147+ function setAndroidConfig ($ closure )
148+ {
128149 $ closure ($ this ->android );
129150 return $ this ;
130151 }
@@ -133,7 +154,8 @@ function setAndroidConfig($closure){
133154 * Set high priority
134155 * @return $this
135156 */
136- function highPriority (){
157+ function highPriority ()
158+ {
137159 $ this ->priority ('high ' );
138160 return $ this ;
139161 }
@@ -142,7 +164,8 @@ function highPriority(){
142164 * Set normal priority
143165 * @return $this
144166 */
145- function normalPriority (){
167+ function normalPriority ()
168+ {
146169 $ this ->priority ('normal ' );
147170 return $ this ;
148171 }
@@ -152,19 +175,76 @@ function normalPriority(){
152175 * @param string $ttl TTL as a string (ex. '14.5s')
153176 * @return $this
154177 */
155- function ttl ($ ttl ){
178+ function ttl ($ ttl )
179+ {
156180 $ this ->message ->android ->ttl ($ ttl );
157181 return $ this ;
158182 }
159183
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+ }
160239
161240 /**
162241 * Set the message destination,
163242 * this field is MANDATORY to submit the message
164243 * @param Target $target
165244 * @return FcmMessageBuilder
166245 */
167- function toTarget (Target $ target ){
246+ function toTarget (Target $ target )
247+ {
168248 $ this ->message ->setTarget ($ target );
169249 return $ this ;
170250 }
@@ -174,7 +254,8 @@ function toTarget(Target $target){
174254 * @param string $token Target device FCM token
175255 * @return FcmMessageBuilder
176256 */
177- function toDevice ($ token ){
257+ function toDevice ($ token )
258+ {
178259 return $ this ->toTarget (new Token ($ token ));
179260 }
180261
@@ -183,7 +264,8 @@ function toDevice($token){
183264 * @param string $topicName Target topic name
184265 * @return FcmMessageBuilder
185266 */
186- function toTopic ($ topicName ){
267+ function toTopic ($ topicName )
268+ {
187269 return $ this ->toTarget (new Topic ($ topicName ));
188270 }
189271
@@ -192,7 +274,8 @@ function toTopic($topicName){
192274 * @param string $condition Target condition
193275 * @return FcmMessageBuilder
194276 */
195- function toCondition ($ condition ){
277+ function toCondition ($ condition )
278+ {
196279 return $ this ->toTarget (new Condition ($ condition ));
197280 }
198281
@@ -201,7 +284,8 @@ function toCondition($condition){
201284 * @internal
202285 * @return Request
203286 */
204- private function request (){
287+ private function request ()
288+ {
205289 return new Request ($ this ->serviceAccount );
206290 }
207291
@@ -212,20 +296,87 @@ private function request(){
212296 * @throws \Plokko\Firebase\FCM\Exceptions\FcmErrorException FCMError exception
213297 * @throws FcmTargetNotSpecifiedException will be thrown if no device target is specified
214298 */
215- function send (){
299+ function send ()
300+ {
216301
217- if ($ this ->message ->target === null ){
302+ if ($ this ->message ->target === null ) {
218303 throw new FcmTargetNotSpecifiedException ();
219304 }
220305 try {
221306 $ 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 );
225333 }
226334 throw $ e ;
227335 }
228336
229337 }
230338
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