8000 [Notifier][Firebase] Add Firebase v1 API support by vojtechsmejkal · Pull Request #60205 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Notifier][Firebase] Add Firebase v1 API support #60205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: 7.4
Choose a base branch
from
Prev Previous commit
Next Next commit
[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.
  • Loading branch information
Vojtech Smejkal committed May 25, 2025
commit 5fddcdde1681c1a64148a132e7fe6cfc91b4fd46
174 changes: 160 additions & 14 deletions src/Symfony/Component/Notifier/Bridge/Firebase/FirebaseOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@
*
* @see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages
*/
abstract class FirebaseOptions implements MessageOptionsInterface
class FirebaseOptions implements MessageOptionsInterface
{
protected array $options;

/**
* @param array<string, string> $data arbitrary meta-data (both keys and values must be a string)
*/
public function __construct(
private string $target,
array $options,
private array $data = [],
protected array $options = [],
array $data = [],
protected TargetType $targetType = TargetType::Topic,
) {
$this->options = $options;
$this->options['data'] = $data;
}

public function toArray(): array
{
return [
$this->targetType->value => $this->target,
'notification' => $this->options,
'data' => $this->data,
];
$options = $this->options;
$options[$this->targetType->value] = $this->target;

return $options;
}

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

return $this;
}
Expand All @@ -61,17 +61,163 @@ public function title(string $title): static
*/
public function body(string $body): static
{
$this->options['body'] = $body;
$this->addNotificationOption('body', $body);

return $this;
}

/**
* @param string $image URL of an image that is going to be downloaded on the device and displayed in a notification
*
* @return $this
*/
public function image(string $image): static
{
$this->addNotificationOption('image', $image);

return $this;
}

/**
* @param array<string, string> $data
*
* @return $this
*/
public function data(array $data): static
{
$this->data = $data;
$this->options['data'] = $data;

return $this;
}

/**
* @param array<string, mixed> $notification
*
* @return $this
*
* @see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#notification
*/
public function notification(array $notification): static
{
$this->options['notification'] = $notification;

return $this;
}

/**
* @param array<string, mixed> $android
*
* @return $this
*
* @see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#androidconfig
*/
public function android(array $android): static
{
$this->options['android'] = $android;

return $this;
}

/**
* @param array<string, mixed> $webpush
*
* @return $this
*
* @see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#webpushconfig
*/
public function webpush(array $webpush): static
{
$this->options['webpush'] = $webpush;

return $this;
}

/**
* @param array<string, mixed> $apns
*
* @return $this
*
* @see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#apnsconfig
*/
public function apns(array $apns): static
{
$this->options['apns'] = $apns;

return $this;
}

/**
* @param array<string, mixed> $fcmOptions
*
* @return $this
*
* @see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#fcmoptions
*/
public function fcmOptions(array $fcmOptions): static
{
$this->options['fcm_options'] = $fcmOptions;

return $this;
}

/**
* @return $this
*/
protected function addNotificationOption(string $key, mixed $value): static
{
$this->options['notification'] ??= [];
$this->options['notification'][$key] = $value;

return $this;
}

/**
* @return $this
*/
protected function addAndroidOption(string $key, mixed $value): static
{
$this->options['android'] ??= [];
$this->options['android']['notification'] ??= [];
$this->options['android']['notification'][$key] = $value;

return $this;
}

/**
* @return $this
*/
protected function addWebpushOption(string $key, mixed $value): static
{
$this->options['webpush'] ??= [];
$this->options['webpush']['notification'] ??= [];
$this->options['webpush']['notification'][$key] = $value;

return $this;
}

/**
* @return $this
*/
protected function addApnsOption(string $key, mixed $value): static
{
$this->options['apns'] ??= [];
$this->options['apns']['payload'] ??= [];
$this->options['apns']['payload']['aps'] ??= [];
$this->options['apns']['payload']['aps'][$key] = $value;

return $this;
}

/**
* @return $this
*/
protected function addApnsAlertOption(string $key, mixed $value): static
{
$this->options['apns'] ??= [];
$this->options['apns']['payload'] ??= [];
$this->options['apns']['payload']['aps'] ??= [];
$this->options['apns']['payload']['aps']['alert'] ??= [];
$this->options['apns']['payload']['aps']['alert'][$key] = $value;

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,77 +12,81 @@
namespace Symfony\Component\Notifier\Bridge\Firebase\Notification;

use Symfony\Component\Notifier\Bridge\Firebase\FirebaseOptions;
use Symfony\Component\Notifier\Bridge\Firebase\TargetType;

final class AndroidNotification extends FirebaseOptions
{
public function __construct(
string $target,
array $options = [],
array $data = [],
TargetType $targetType = TargetType::Topic,
) {
\trigger_deprecation(
'symfony/firebase-notifier',
'7.3',
'Using %s class is deprecated, use %s instead.',
self::class,
FirebaseOptions::class,
);

parent::__construct($target, ['notification' => $options], $data, $targetType);
}

/**
* @return $this
*/
public function channelId(string $channelId): static
{
$this->options['android_channel_id'] = $channelId;

return $this;
return $this->addAndroidOption('channel_id', $channelId);
}

/**
* @return $this
*/
public function icon(string $icon): static
{
$this->options['icon'] = $icon;

return $this;
return $this->addAndroidOption('icon', $icon);
}

/**
* @return $this
*/
public function sound(string $sound): static
{
$this->options['sound'] = $sound;

return $this;
return $this->addAndroidOption('sound', $sound);
}

/**
* @return $this
*/
public function tag(string $tag): static
{
$this->options['tag'] = $tag;

return $this;
return $this->addAndroidOption('tag', $tag);
}

/**
* @return $this
*/
public function color(string $color): static
{
$this->options['color'] = $color;

return $this;
return $this->addAndroidOption('color', $color);
}

/**
* @return $this
*/
public function clickAction(string $clickAction): static
{
$this->options['click_action'] = $clickAction;

return $this;
return $this->addAndroidOption('click_action', $clickAction);
}

/**
* @return $this
*/
public function bodyLocKey(string $bodyLocKey): static
{
$this->options['body_loc_key'] = $bodyLocKey;

return $this;
return $this->addAndroidOption('body_loc_key', $bodyLocKey);
}

/**
Expand All @@ -92,19 +96,15 @@ public function bodyLocKey(string $bodyLocKey): static
*/
public function bodyLocArgs(array $bodyLocArgs): static
{
$this->options['body_loc_args'] = $bodyLocArgs;

return $this;
return $this->addAndroidOption('body_loc_args', $bodyLocArgs);
}

/**
* @return $this
*/
public function titleLocKey(string $titleLocKey): static
{
$this->options['title_loc_key'] = $titleLocKey;

return $this;
return $this->addAndroidOption('title_loc_key', $titleLocKey);
}

/**
Expand All @@ -114,8 +114,6 @@ public function titleLocKey(string $titleLocKey): static
*/
public function titleLocArgs(array $titleLocArgs): static
{
$this->options['title_loc_args'] = $titleLocArgs;

return $this;
return $this->addAndroidOption('title_loc_args', $titleLocArgs);
}
}
Loading
0