19
19
*
20
20
* @see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages
21
21
*/
22
- abstract class FirebaseOptions implements MessageOptionsInterface
22
+ class FirebaseOptions implements MessageOptionsInterface
23
23
{
24
- protected array $ options ;
25
-
24
+ /**
25
+ * @param array<string, string> $data arbitrary meta-data (both keys and values must be a string)
26
+ */
26
27
public function __construct (
27
28
private string $ target ,
28
- array $ options ,
29
- private array $ data = [],
29
+ protected array $ options ,
30
+ array $ data = [],
30
31
protected TargetType $ targetType = TargetType::Topic,
31
32
) {
32
- $ this ->options = $ options ;
33
+ $ this ->options [ ' data ' ] = $ data ;
33
34
}
34
35
35
36
public function toArray (): array
36
37
{
37
38
return [
39
+ ...$ this ->options ,
38
40
$ this ->targetType ->value => $ this ->target ,
39
- 'notification ' => $ this ->options ,
40
- 'data ' => $ this ->data ,
41
41
];
42
42
}
43
43
@@ -51,7 +51,7 @@ public function getRecipientId(): ?string
51
51
*/
52
52
public function title (string $ title ): static
53
53
{
54
- $ this ->options [ 'title ' ] = $ title ;
54
+ $ this ->addNotificationOption ( 'title ' , $ title) ;
55
55
56
56
return $ this ;
57
57
}
@@ -61,17 +61,163 @@ public function title(string $title): static
61
61
*/
62
62
public function body (string $ body ): static
63
63
{
64
- $ this ->options ['body ' ] = $ body ;
64
+ $ this ->addNotificationOption ('body ' , $ body );
65
+
66
+ return $ this ;
67
+ }
68
+
69
+ /**
70
+ * @param string $image URL of an image that is going to be downloaded on the device and displayed in a notification
71
+ *
72
+ * @return $this
73
+ */
74
+ public function image (string $ image ): static
75
+ {
76
+ $ this ->addNotificationOption ('image ' , $ image );
65
77
66
78
return $ this ;
67
79
}
68
80
69
81
/**
82
+ * @param array<string, string> $data
83
+ *
70
84
* @return $this
71
85
*/
72
86
public function data (array $ data ): static
73
87
{
74
- $ this ->data = $ data ;
88
+ $ this ->options ['data ' ] = $ data ;
89
+
90
+ return $ this ;
91
+ }
92
+
93
+ /**
94
+ * @param array<string, mixed> $notification
95
+ *
96
+ * @return $this
97
+ *
98
+ * @see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#notification
99
+ */
100
+ public function notification (array $ notification ): static
101
+ {
102
+ $ this ->options ['notification ' ] = $ notification ;
103
+
104
+ return $ this ;
105
+ }
106
+
107
+ /**
108
+ * @param array<string, mixed> $android
109
+ *
110
+ * @return $this
111
+ *
112
+ * @see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#androidconfig
113
+ */
114
+ public function android (array $ android ): static
115
+ {
116
+ $ this ->options ['android ' ] = $ android ;
117
+
118
+ return $ this ;
119
+ }
120
+
121
+ /**
122
+ * @param array<string, mixed> $webpush
123
+ *
124
+ * @return $this
125
+ *
126
+ * @see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#webpushconfig
127
+ */
128
+ public function webpush (array $ webpush ): static
129
+ {
130
+ $ this ->options ['webpush ' ] = $ webpush ;
131
+
132
+ return $ this ;
133
+ }
134
+
135
+ /**
136
+ * @param array<string, mixed> $apns
137
+ *
138
+ * @return $this
139
+ *
140
+ * @see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#apnsconfig
141
+ */
142
+ public function apns (array $ apns ): static
143
+ {
144
+ $ this ->options ['apns ' ] = $ apns ;
145
+
146
+ return $ this ;
147
+ }
148
+
149
+ /**
150
+ * @param array<string, mixed> $fcmOptions
151
+ *
152
+ * @return $this
153
+ *
154
+ * @see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#fcmoptions
155
+ */
156
+ public function fcmOptions (array $ fcmOptions ): static
157
+ {
158
+ $ this ->options ['fcm_options ' ] = $ fcmOptions ;
159
+
160
+ return $ this ;
161
+ }
162
+
163
+ /**
164
+ * @return $this
165
+ */
166
+ protected function addNotificationOption (string $ key , mixed $ value ): static
167
+ {
168
+ $ this ->options ['notification ' ] ??= [];
169
+ $ this ->options ['notification ' ][$ key ] = $ value ;
170
+
171
+ return $ this ;
172
+ }
173
+
174
+ /**
175
+ * @return $this
176
+ */
177
+ protected function addAndroidOption (string $ key , mixed $ value ): static
178
+ {
179
+ $ this ->options ['android ' ] ??= [];
180
+ $ this ->options ['android ' ]['notification ' ] ??= [];
181
+ $ this ->options ['android ' ]['notification ' ][$ key ] = $ value ;
182
+
183
+ return $ this ;
184
+ }
185
+
186
+ /**
187
+ * @return $this
188
+ */
189
+ protected function addWebpushOption (string $ key , mixed $ value ): static
190
+ {
191
+ $ this ->options ['webpush ' ] ??= [];
192
+ $ this ->options ['webpush ' ]['notification ' ] ??= [];
193
+ $ this ->options ['webpush ' ]['notification ' ][$ key ] = $ value ;
194
+
195
+ return $ this ;
196
+ }
197
+
198
+ /**
199
+ * @return $this
200
+ */
201
+ protected function addApnsOption (string $ key , mixed $ value ): static
202
+ {
203
+ $ this ->options ['apns ' ] ??= [];
204
+ $ this ->options ['apns ' ]['payload ' ] ??= [];
205
+ $ this ->options ['apns ' ]['payload ' ]['aps ' ] ??= [];
206
+ $ this ->options ['apns ' ]['payload ' ]['aps ' ][$ key ] = $ value ;
207
+
208
+ return $ this ;
209
+ }
210
+
211
+ /**
212
+ * @return $this
213
+ */
214
+ protected function addApnsAlertOption (string $ key , mixed $ value ): static
215
+ {
216
+ $ this ->options ['apns ' ] ??= [];
217
+ $ this ->options ['apns ' ]['payload ' ] ??= [];
218
+ $ this ->options ['apns ' ]['payload ' ]['aps ' ] ??= [];
219
+ $ this ->options ['apns ' ]['payload ' ]['aps ' ]['alert ' ] ??= [];
220
+ $ this ->options ['apns ' ]['payload ' ]['aps ' ]['alert ' ][$ key ] = $ value ;
75
221
76
222
return $ this ;
77
223
}
0 commit comments