8000 [Notifier][Firebase] Rework FirebaseOptions to better reflect new API… · symfony/symfony@f15fedf · GitHub
[go: up one dir, main page]

Skip to content

Commit f15fedf

Browse files
author
Vojtech Smejkal
committed
[Notifier][Firebase] Rework FirebaseOptions to better reflect new API shape
Shape of the endpoint input has changed with the new API. Separating options for messages based on the target platform no longer makes sense as all options for all platforms can now be set for the same message. AndroidNotification, IOSNotification and WebNotification were marked as deprecated and will be removed. FirebaseOptions is no longer abstract and should be used directly.
1 parent 9e0f34e commit f15fedf

File tree

4 files changed

+231
-71
lines changed

4 files changed

+231
-71
lines changed

src/Symfony/Component/Notifier/Bridge/Firebase/FirebaseOptions.php

Lines changed: 157 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@
1919
*
2020
* @see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages
2121
*/
22-
abstract class FirebaseOptions implements MessageOptionsInterface
22+
class FirebaseOptions implements MessageOptionsInterface
2323
{
24-
protected array $options;
25-
24+
/**
25+
* @param array<string, string> $data arbitrary meta-data (both keys and values must be a string)
26+
*/
2627
public function __construct(
2728
private string $target,
28-
array $options,
29-
private array $data = [],
29+
protected array $options,
30+
array $data = [],
3031
protected TargetType $targetType = TargetType::Topic,
3132
) {
32-
$this->options = $options;
33+
$this->options['data'] = $data;
3334
}
3435

3536
public function toArray(): array
3637
{
3738
return [
39+
...$this->options,
3840
$this->targetType->value => $this->target,
39-
'notification' => $this->options,
40-
'data' => $this->data,
4141
];
4242
}
4343

@@ -51,7 +51,7 @@ public function getRecipientId(): ?string
5151
*/
5252
public function title(string $title): static
5353
{
54-
$this->options['title'] = $title;
54+
$this->addNotificationOption('title', $title);
5555

5656
return $this;
5757
}
@@ -61,17 +61,163 @@ public function title(string $title): static
6161
*/
6262
public function body(string $body): static
6363
{
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);
6577

6678
return $this;
6779
}
6880

6981
/**
82+
* @param array<string, string> $data
83+
*
7084
* @return $this
7185
*/
7286
public function data(array $data): static
7387
{
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;
75221

76222
return $this;
77223
}

src/Symfony/Component/Notifier/Bridge/Firebase/Notification/AndroidNotification.php

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,77 +12,81 @@
1212
namespace Symfony\Component\Notifier\Bridge\Firebase\Notification;
1313

1414
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseOptions;
15+
use Symfony\Component\Notifier\Bridge\Firebase\TargetType;
1516

1617
final class AndroidNotification extends FirebaseOptions
1718
{
19+
public function __construct(
20+
string $target,
21+
array $options,
22+
array $data = [],
23+
TargetType $targetType = TargetType::Topic,
24+
) {
25+
trigger_deprecation(
26+
'symfony/firebase-notifier',
27+
'7.3',
28+
'Using %s class is deprecated, use %s instead.',
29+
self::class,
30+
FirebaseOptions::class,
31+
);
32+
33+
parent::__construct($target, ['notification' => $options], $data, $targetType);
34+
}
35+
1836
/**
1937
* @return $this
2038
*/
2139
public function channelId(string $channelId): static
2240
{
23-
$this->options['android_channel_id'] = $channelId;
24-
25-
return $this;
41+
return $this->addAndroidOption('channel_id', $channelId);
2642
}
2743

2844
/**
2945
* @return $this
3046
*/
3147
public function icon(string $icon): static
3248
{
33-
$this->options['icon'] = $icon;
34-
35-
return $this;
49+
return $this->addAndroidOption('icon', $icon);
3650
}
3751

3852
/**
3953
* @return $this
4054
*/
4155
public function sound(string $sound): static
4256
{
43-
$this->options['sound'] = $sound;
44-
45-
return $this;
57+
return $this->addAndroidOption('sound', $sound);
4658
}
4759

4860
/**
4961
* @return $this
5062
*/
5163
public function tag(string $tag): static
5264
{
53-
$this->options['tag'] = $tag;
54-
55-
return $this;
65+
return $this->addAndroidOption('tag', $tag);
5666
}
5767

5868
/**
5969
* @return $this
6070
*/
6171
public function color(string $color): static
6272
{
63-
$this->options['color'] = $color;
64-
65-
return $this;
73+
return $this->addAndroidOption('color', $color);
6674
}
6775

6876
/**
6977
* @return $this
7078
*/
7179
public function clickAction(string $clickAction): static
7280
{
73-
$this->options['click_action'] = $clickAction;
74-
75-
return $this;
81+
return $this->addAndroidOption('click_action', $clickAction);
7682
}
7783

7884
/**
7985
* @return $this
8086
*/
8187
public function bodyLocKey(string $bodyLocKey): static
8288
{
83-
$this->options['body_loc_key'] = $bodyLocKey;
84-
85-
return $this;
89+
return $this->addAndroidOption('body_loc_key', $bodyLocKey);
8690
}
8791

8892
/**
@@ -92,19 +96,15 @@ public function bodyLocKey(string $bodyLocKey): static
9296
*/
9397
public function bodyLocArgs(array $bodyLocArgs): static
9498
{
95-
$this->options['body_loc_args'] = $bodyLocArgs;
96-
97-
return $this;
99+
return $this->addAndroidOption('body_loc_args', $bodyLocArgs);
98100
}
99101

100102
/**
101103
* @return $this
102104
*/
103105
public function titleLocKey(string $titleLocKey): static
104106
{
105-
$this->options['title_loc_key'] = $titleLocKey;
106-
107-
return $this;
107+
return $this->addAndroidOption('title_loc_key', $titleLocKey);
108108
}
109109

110110
/**
@@ -114,8 +114,6 @@ public function titleLocKey(string $titleLocKey): static
114114
*/
115115
public function titleLocArgs(array $titleLocArgs): static
116116
{
117-
$this->options['title_loc_args'] = $titleLocArgs;
118-
119-
return $this;
117+
return $this->addAndroidOption('title_loc_args', $titleLocArgs);
120118
}
121119
}

0 commit comments

Comments
 (0)
0