diff --git a/README.md b/README.md index 4dd8d1b..e54c302 100644 --- a/README.md +++ b/README.md @@ -50,11 +50,7 @@ it via the `FcmChannel::class`. Here is an example: use Illuminate\Notifications\Notification; use NotificationChannels\Fcm\FcmChannel; use NotificationChannels\Fcm\FcmMessage; -use NotificationChannels\Fcm\Resources\AndroidConfig; -use NotificationChannels\Fcm\Resources\AndroidFcmOptions; -use NotificationChannels\Fcm\Resources\AndroidNotification; -use NotificationChannels\Fcm\Resources\ApnsConfig; -use NotificationChannels\Fcm\Resources\ApnsFcmOptions; +use NotificationChannels\Fcm\Resources\Notification as FcmNotification; class AccountActivated extends Notification { @@ -65,19 +61,27 @@ class AccountActivated extends Notification public function toFcm($notifiable) { - return FcmMessage::create() - ->setData(['data1' => 'value', 'data2' => 'value2']) - ->setNotification(\NotificationChannels\Fcm\Resources\Notification::create() - ->setTitle('Account Activated') - ->setBody('Your account has been activated.') - ->setImage('http://example.com/url-to-image-here.png')) - ->setAndroid( - AndroidConfig::create() - ->setFcmOptions(AndroidFcmOptions::create()->setAnalyticsLabel('analytics')) - ->setNotification(AndroidNotification::create()->setColor('#0A0A0A')) - )->setApns( - ApnsConfig::create() - ->setFcmOptions(ApnsFcmOptions::create()->setAnalyticsLabel('analytics_ios'))); + return new FcmMessage(notification: new FcmNotification( + title: 'Account Activated', + body: 'Your account has been activated.', + image: 'http://example.com/url-to-image-here.png' + )) + ->data(['data1' => 'value', 'data2' => 'value2']) + ->custom([ + 'android' => [ + 'notification' => [ + 'color' => '#0A0A0A', + ], + 'fcm_options' => [ + 'analytics_label' => 'analytics', + ], + ], + 'apns' => [ + 'fcm_options' => [ + 'analytics_label' => 'analytics', + ], + ], + ]); } } ``` @@ -132,45 +136,16 @@ $user->notify(new AccountActivated); ### Available Message methods -The `FcmMessage` class contains the following methods for defining the payload. All these methods correspond to the -available payload defined in the -[FCM API documentation](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages). Refer to this link to -find all the available data you can set in your FCM notification. +View the `FcmMessage` source for the complete list of options. ```php -setName(string $name) -``` - -```php -setData(array $data) -``` - -```php -setNotification(\NotificationChannels\Fcm\Resources\Notification $notification) -``` - -```php -setAndroid(NotificationChannels\Fcm\Resources\AndroidConfig $androidConfig) -``` - -```php -setApns(NotificationChannels\Fcm\Resources\ApnsConfig $apnsConfig) -``` - -```php -setWebpush(NotificationChannels\Fcm\Resources\WebpushConfig $webpushConfig) -``` - -```php -setFcmOptions(NotificationChannels\Fcm\Resources\FcmOptions $fcmOptions) -``` - -```php -setTopic(string $topic) -``` - -```php -setCondition(string $condition) +FcmMessage::create() + ->name('name') + ->token('token') + ->topic('topic') + ->condition('condition') + ->data(['a' => 'b']) + ->custom(['notification' => []]); ``` ## Custom clients diff --git a/src/Exceptions/InvalidPropertyException.php b/src/Exceptions/InvalidPropertyException.php deleted file mode 100644 index 2144a82..0000000 --- a/src/Exceptions/InvalidPropertyException.php +++ /dev/null @@ -1,13 +0,0 @@ -chunk(self::TOKENS_PER_REQUEST) - ->map(fn ($tokens) => ($fcmMessage->getClient() ?? $this->client)->sendMulticast($fcmMessage, $tokens->all())) + ->map(fn ($tokens) => ($fcmMessage->client ?? $this->client)->sendMulticast($fcmMessage, $tokens->all())) ->map(fn (MulticastSendReport $report) => $this->checkReportForFailures($notifiable, $notification, $report)); } diff --git a/src/FcmMessage.php b/src/FcmMessage.php index aa611c1..b0d65ef 100644 --- a/src/FcmMessage.php +++ b/src/FcmMessage.php @@ -5,92 +5,40 @@ use Illuminate\Support\Traits\Macroable; use Kreait\Firebase\Contract\Messaging; use Kreait\Firebase\Messaging\Message; -use NotificationChannels\Fcm\Exceptions\InvalidPropertyException; -use NotificationChannels\Fcm\Resources\AndroidConfig; -use NotificationChannels\Fcm\Resources\ApnsConfig; -use NotificationChannels\Fcm\Resources\FcmOptions; use NotificationChannels\Fcm\Resources\Notification; -use NotificationChannels\Fcm\Resources\WebpushConfig; class FcmMessage implements Message { use Macroable; /** - * @var string|null + * Create a new message instance. */ - protected $name; - - /** - * @var array|null - */ - protected $data; - - /** - * @var Notification|null - */ - protected $notification; - - /** - * @var AndroidConfig|null - */ - protected $android; - - /** - * @var WebpushConfig|null - */ - protected $webpush; - - /** - * @var ApnsConfig|null - */ - protected $apns; - - /** - * @var FcmOptions|null - */ - protected $fcmOptions; - - /** - * @var string|null - */ - protected $token; - - /** - * @var string|null - */ - protected $topic; - - /** - * @var string|null - */ - protected $condition; - - /** - * The custom messaging client. - * - * @var \Kreait\Firebase\Contract\Messaging - */ - protected $client; - - public static function create(): self - { - return new self; + public function __construct( + public ?string $name = null, + public ?string $token = null, + public ?string $topic = null, + public ?string $condition = null, + public ?array $data = [], + public array $custom = [], + public ?Notification $notification = null, + public ?Messaging $client = null, + ) { + // } /** - * @return string|null + * Create a new message instance. */ - public function getName(): ?string + public static function create(...$args): static { - return $this->name; + return new static(...$args); } /** - * @param string|null $name - * @return $this + * Set the message name. */ - public function setName(?string $name): self + public function name(?string $name): self { $this->name = $name; @@ -98,201 +46,69 @@ public function setName(?string $name): self } /** - * @return array|null - */ - public function getData(): ?array - { - return $this->data; - } - - /** - * @param array|null $data - * @return $this - * - * @throws \NotificationChannels\Fcm\Exceptions\InvalidPropertyException - */ - public function setData(?array $data): self - { - foreach ($data as $key => $item) { - if (! is_string($item)) { - throw InvalidPropertyException::mustBeString($key); - } - } - - $this->data = $data; - - return $this; - } - - /** - * @return Notification|null - */ - public function getNotification(): ?Notification - { - return $this->notification; - } - - /** - * @param Notification|null $notification - * @return $this - */ - public function setNotification(?Notification $notification): self - { - $this->notification = $notification; - - return $this; - } - - /** - * @return AndroidConfig|null + * Set the message token. */ - public function getAndroid(): ?AndroidConfig + public function token(?string $token): self { - return $this->android; - } - - /** - * @param AndroidConfig|null $android - * @return $this - */ - public function setAndroid(?AndroidConfig $android): self - { - $this->android = $android; - - return $this; - } - - /** - * @return WebpushConfig|null - */ - public function getWebpush(): ?WebpushConfig - { - return $this->webpush; - } - - /** - * @param WebpushConfig|null $webpush - * @return $this - */ - public function setWebpush(?WebpushConfig $webpush): self - { - $this->webpush = $webpush; + $this->token = $token; return $this; } /** - * @return ApnsConfig|null - */ - public function getApns(): ?ApnsConfig - { - return $this->apns; - } - - /** - * @param ApnsConfig|null $apns - * @return $this + * Set the message topic.s */ - public function setApns(?ApnsConfig $apns): self + public function topic(?string $topic): self { - $this->apns = $apns; + $this->topic = $topic; return $this; } /** - * @return FcmOptions|null + * Set the message condition. */ - public function getFcmOptions(): ?FcmOptions + public function condition(?string $condition): self { - return $this->fcmOptions; - } - - /** - * @param FcmOptions|null $fcmOptions - * @return $this - */ - public function setFcmOptions(?FcmOptions $fcmOptions): self - { - $this->fcmOptions = $fcmOptions; + $this->condition = $condition; return $this; } /** - * @return string|null + * Set the message data. */ - public function getToken(): ?string + public function data(?array $data): self { - return $this->token; - } - - /** - * @param string|null $token - * @return $this - */ - public function setToken(?string $token): self - { - $this->token = $token; + $this->data = $data; return $this; } /** - * @return string|null + * Set additional custom message data. */ - public function getTopic(): ?string + public function custom(?array $custom): self { - return $this->topic; - } - - /** - * @param string|null $topic - * @return $this - */ - public function setTopic(?string $topic): self - { - $this->topic = $topic; + $this->custom = $custom; return $this; } /** - * @return string|null + * Set the message notification. */ - public function getCondition(): ?string + public function notification(Notification $notification): self { - return $this->condition; - } - - /** - * @param string|null $condition - * @return $this - */ - public function setCondition(?string $condition): self - { - $this->condition = $condition; + $this->notification = $notification; return $this; } /** - * Get the custom Firebase Messaging client. - * - * @return \Kreait\Firebase\Contract\Messaging|null - */ - public function getClient(): ?Messaging - { - return $this->client; - } - - /** - * Set the custom Firebase Messaging client instance. - * - * @param \Kreait\Firebase\Contract\Messaging $client - * @return $this + * Set the message Firebase Messaging client instance. */ - public function usingClient(Messaging $client) + public function usingClient(Messaging $client): self { $this->client = $client; @@ -301,29 +117,15 @@ public function usingClient(Messaging $client) public function toArray() { - $data = [ - 'name' => $this->getName(), - 'data' => $this->getData(), - 'notification' => ! is_null($this->getNotification()) ? $this->getNotification()->toArray() : null, - 'android' => ! is_null($this->getAndroid()) ? $this->getAndroid()->toArray() : null, - 'webpush' => ! is_null($this->getWebpush()) ? $this->getWebpush()->toArray() : null, - 'apns' => ! is_null($this->getApns()) ? $this->getApns()->toArray() : null, - 'fcm_options' => ! is_null($this->getFcmOptions()) ? $this->getFcmOptions()->toArray() : null, - ]; - - if ($token = $this->getToken()) { - $data['token'] = $token; - } - - if ($topic = $this->getTopic()) { - $data['topic'] = $topic; - } - - if ($condition = $this->getCondition()) { - $data['condition'] = $condition; - } - - return $data; + return array_filter([ + 'name' => $this->name, + 'data' => $this->data, + 'token' => $this->token, + 'topic' => $this->topic, + 'condition' => $this->condition, + 'notification' => $this->notification?->toArray(), + ...$this->custom, + ]); } /** diff --git a/src/Resources/AndroidConfig.php b/src/Resources/AndroidConfig.php deleted file mode 100644 index 4399a77..0000000 --- a/src/Resources/AndroidConfig.php +++ /dev/null @@ -1,208 +0,0 @@ -collapseKey; - } - - /** - * @param string|null $collapseKey - * @return AndroidConfig - */ - public function setCollapseKey(?string $collapseKey): self - { - $this->collapseKey = $collapseKey; - - return $this; - } - - /** - * @return AndroidMessagePriority|null - */ - public function getPriority(): ?AndroidMessagePriority - { - return $this->priority; - } - - /** - * @param AndroidMessagePriority|null $priority - * @return AndroidConfig - */ - public function setPriority(?AndroidMessagePriority $priority): self - { - $this->priority = $priority; - - return $this; - } - - /** - * @return string|null - */ - public function getTtl(): ?string - { - return $this->ttl; - } - - /** - * @param string|null $ttl - * @return AndroidConfig - */ - public function setTtl(?string $ttl): self - { - $this->ttl = $ttl; - - return $this; - } - - /** - * @return string|null - */ - public function getRestrictedPackageName(): ?string - { - return $this->restrictedPackageName; - } - - /** - * @param string|null $restrictedPackageName - * @return AndroidConfig - */ - public function setRestrictedPackageName(?string $restrictedPackageName): self - { - $this->restrictedPackageName = $restrictedPackageName; - - return $this; - } - - /** - * @return array|null - */ - public function getData(): ?array - { - return $this->data; - } - - /** - * @param array|null $data - * @return AndroidConfig - * - * @throws \NotificationChannels\Fcm\Exceptions\InvalidPropertyException - */ - public function setData(?array $data): self - { - foreach ($data as $key => $item) { - if (! is_string($item)) { - throw InvalidPropertyException::mustBeString($key); - } - } - - $this->data = $data; - - return $this; - } - - /** - * @return AndroidNotification|null - */ - public function getNotification(): ?AndroidNotification - { - return $this->notification; - } - - /** - * @param AndroidNotification|null $notification - * @return AndroidConfig - */ - public function setNotification(?AndroidNotification $notification): self - { - $this->notification = $notification; - - return $this; - } - - /** - * @return AndroidFcmOptions|null - */ - public function getFcmOptions(): ?AndroidFcmOptions - { - return $this->fcmOptions; - } - - /** - * @param AndroidFcmOptions|null $fcmOptions - * @return AndroidConfig - */ - public function setFcmOptions(?AndroidFcmOptions $fcmOptions): self - { - $this->fcmOptions = $fcmOptions; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function toArray(): array - { - return [ - 'collapse_key' => $this->getCollapseKey(), - 'priority' => $this->getPriority()?->name, - 'ttl' => $this->getTtl(), - 'restricted_package_name' => $this->getRestrictedPackageName(), - 'data' => $this->getData(), - 'notification' => ! is_null($this->getNotification()) ? $this->getNotification()->toArray() : null, - 'fcm_options' => ! is_null($this->getFcmOptions()) ? $this->getFcmOptions()->toArray() : null, - ]; - } -} diff --git a/src/Resources/AndroidFcmOptions.php b/src/Resources/AndroidFcmOptions.php deleted file mode 100644 index 5c0d292..0000000 --- a/src/Resources/AndroidFcmOptions.php +++ /dev/null @@ -1,48 +0,0 @@ -analyticsLabel; - } - - /** - * @param string|null $analyticsLabel - * @return AndroidFcmOptions - */ - public function setAnalyticsLabel(?string $analyticsLabel): self - { - $this->analyticsLabel = $analyticsLabel; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function toArray(): array - { - return [ - 'analytics_label' => $this->getAnalyticsLabel(), - ]; - } -} diff --git a/src/Resources/AndroidMessagePriority.php b/src/Resources/AndroidMessagePriority.php deleted file mode 100644 index 66d554b..0000000 --- a/src/Resources/AndroidMessagePriority.php +++ /dev/null @@ -1,9 +0,0 @@ -title; - } - - /** - * @param string|null $title - * @return AndroidNotification - */ - public function setTitle(?string $title): self - { - $this->title = $title; - - return $this; - } - - /** - * @return string|null - */ - public function getBody(): ?string - { - return $this->body; - } - - /** - * @param string|null $body - * @return AndroidNotification - */ - public function setBody(?string $body): self - { - $this->body = $body; - - return $this; - } - - /** - * @return string|null - */ - public function getIcon(): ?string - { - return $this->icon; - } - - /** - * @param string|null $icon - * @return AndroidNotification - */ - public function setIcon(?string $icon): self - { - $this->icon = $icon; - - return $this; - } - - /** - * @return string|null - */ - public function getColor(): ?string - { - return $this->color; - } - - /** - * @param string|null $color - * @return AndroidNotification - */ - public function setColor(?string $color): self - { - $this->color = $color; - - return $this; - } - - /** - * @return string|null - */ - public function getSound(): ?string - { - return $this->sound; - } - - /** - * @param string|null $sound - * @return AndroidNotification - */ - public function setSound(?string $sound): self - { - $this->sound = $sound; - - return $this; - } - - /** - * @return string|null - */ - public function getTag(): ?string - { - return $this->tag; - } - - /** - * @param string|null $tag - * @return AndroidNotification - */ - public function setTag(?string $tag): self - { - $this->tag = $tag; - - return $this; - } - - /** - * @return string|null - */ - public function getClickAction(): ?string - { - return $this->clickAction; - } - - /** - * @param string|null $clickAction - * @return AndroidNotification - */ - public function setClickAction(?string $clickAction): self - { - $this->clickAction = $clickAction; - - return $this; - } - - /** - * @return string|null - */ - public function getBodyLocKey(): ?string - { - return $this->bodyLocKey; - } - - /** - * @param string|null $bodyLocKey - * @return AndroidNotification - */ - public function setBodyLocKey(?string $bodyLocKey): self - { - $this->bodyLocKey = $bodyLocKey; - - return $this; - } - - /** - * @return string[]|null - */ - public function getBodyLocArgs(): ?array - { - return $this->bodyLocArgs; - } - - /** - * @param string[]|null $bodyLocArgs - * @return AndroidNotification - */ - public function setBodyLocArgs(?array $bodyLocArgs): self - { - $this->bodyLocArgs = $bodyLocArgs; - - return $this; - } - - /** - * @return string|null - */ - public function getTitleLocKey(): ?string - { - return $this->titleLocKey; - } - - /** - * @param string|null $titleLocKey - * @return AndroidNotification - */ - public function setTitleLocKey(?string $titleLocKey): self - { - $this->titleLocKey = $titleLocKey; - - return $this; - } - - /** - * @return string[]|null - */ - public function getTitleLocArgs(): ?array - { - return $this->titleLocArgs; - } - - /** - * @param string[]|null $titleLocArgs - * @return AndroidNotification - */ - public function setTitleLocArgs(?array $titleLocArgs): self - { - $this->titleLocArgs = $titleLocArgs; - - return $this; - } - - /** - * @return string|null - */ - public function getChannelId(): ?string - { - return $this->channelId; - } - - /** - * @param string|null $channelId - * @return AndroidNotification - */ - public function setChannelId(?string $channelId): self - { - $this->channelId = $channelId; - - return $this; - } - - /** - * @return string|null - */ - public function getTicker(): ?string - { - return $this->ticker; - } - - /** - * @param string|null $ticker - * @return AndroidNotification - */ - public function setTicker(?string $ticker): self - { - $this->ticker = $ticker; - - return $this; - } - - /** - * @return bool|null - */ - public function getSticky(): ?bool - { - return $this->sticky; - } - - /** - * @param bool|null $sticky - * @return AndroidNotification - */ - public function setSticky(?bool $sticky): self - { - $this->sticky = $sticky; - - return $this; - } - - /** - * @return string|null - */ - public function getEventTime(): ?string - { - return $this->eventTime; - } - - /** - * @param string|null $eventTime - * @return AndroidNotification - */ - public function setEventTime(?string $eventTime): self - { - $this->eventTime = $eventTime; - - return $this; - } - - /** - * @return bool|null - */ - public function getLocalOnly(): ?bool - { - return $this->localOnly; - } - - /** - * @param bool|null $localOnly - * @return AndroidNotification - */ - public function setLocalOnly(?bool $localOnly): self - { - $this->localOnly = $localOnly; - - return $this; - } - - /** - * @return NotificationPriority|null - */ - public function getNotificationPriority(): ?NotificationPriority - { - return $this->notificationPriority; - } - - /** - * @param NotificationPriority|null $notificationPriority - * @return AndroidNotification - */ - public function setNotificationPriority(?NotificationPriority $notificationPriority): self - { - $this->notificationPriority = $notificationPriority; - - return $this; - } - - /** - * @return bool|null - */ - public function getDefaultSound(): ?bool - { - return $this->defaultSound; - } - - /** - * @param bool|null $defaultSound - * @return AndroidNotification - */ - public function setDefaultSound(?bool $defaultSound): self - { - $this->defaultSound = $defaultSound; - - return $this; - } - - /** - * @return bool|null - */ - public function getDefaultVibrateTimings(): ?bool - { - return $this->defaultVibrateTimings; - } - - /** - * @param bool|null $defaultVibrateTimings - * @return AndroidNotification - */ - public function setDefaultVibrateTimings(?bool $defaultVibrateTimings): self - { - $this->defaultVibrateTimings = $defaultVibrateTimings; - - return $this; - } - - /** - * @return bool|null - */ - public function getDefaultLightSettings(): ?bool - { - return $this->defaultLightSettings; - } - - /** - * @param bool|null $defaultLightSettings - * @return AndroidNotification - */ - public function setDefaultLightSettings(?bool $defaultLightSettings): self - { - $this->defaultLightSettings = $defaultLightSettings; - - return $this; - } - - /** - * @return string[]|null - */ - public function getVibrateTimings(): ?array - { - return $this->vibrateTimings; - } - - /** - * @param string[]|null $vibrateTimings - * @return AndroidNotification - */ - public function setVibrateTimings(?array $vibrateTimings): self - { - $this->vibrateTimings = $vibrateTimings; - - return $this; - } - - /** - * @return Visibility|null - */ - public function getVisibility(): ?Visibility - { - return $this->visibility; - } - - /** - * @param Visibility|null $visibility - * @return AndroidNotification - */ - public function setVisibility(?Visibility $visibility): self - { - $this->visibility = $visibility; - - return $this; - } - - /** - * @return int|null - */ - public function getNotificationCount(): ?int - { - return $this->notificationCount; - } - - /** - * @param int|null $notificationCount - * @return AndroidNotification - */ - public function setNotificationCount(?int $notificationCount): self - { - $this->notificationCount = $notificationCount; - - return $this; - } - - /** - * @return LightSettings|null - */ - public function getLightSettings(): ?LightSettings - { - return $this->lightSettings; - } - - /** - * @param LightSettings|null $lightSettings - * @return AndroidNotification - */ - public function setLightSettings(?LightSettings $lightSettings): self - { - $this->lightSettings = $lightSettings; - - return $this; - } - - /** - * @return string|null - */ - public function getImage(): ?string - { - return $this->image; - } - - /** - * @param string|null $image - * @return AndroidNotification - */ - public function setImage(?string $image): self - { - $this->image = $image; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function toArray(): array - { - return [ - 'title' => $this->getTitle(), - 'body' => $this->getBody(), - 'icon' => $this->getIcon(), - 'color' => $this->getColor(), - 'sound' => $this->getSound(), - 'tag' => $this->getTag(), - 'click_action' => $this->getClickAction(), - 'body_loc_key' => $this->getBodyLocKey(), - 'body_loc_args' => $this->getBodyLocArgs(), - 'title_loc_key' => $this->getTitleLocKey(), - 'title_loc_args' => $this->getTitleLocArgs(), - 'channel_id' => $this->getChannelId(), - 'ticker' => $this->getTicker(), - 'sticky' => $this->getSticky(), - 'event_time' => $this->getEventTime(), - 'local_only' => $this->getLocalOnly(), - 'notification_priority' => $this->getNotificationPriority()?->name, - 'default_sound' => $this->getDefaultSound(), - 'default_vibrate_timings' => $this->getDefaultVibrateTimings(), - 'default_light_settings' => $this->getDefaultLightSettings(), - 'vibrate_timings' => $this->getVibrateTimings(), - 'visibility' => $this->getVisibility()?->value, - 'notification_count' => $this->getNotificationCount(), - // 'light_setings' => ! is_null($this->getLightSettings()) ? $this->getLightSettings()->toArray() : null, - 'image' => $this->getImage(), - ]; - } -} diff --git a/src/Resources/ApnsConfig.php b/src/Resources/ApnsConfig.php deleted file mode 100644 index 331f7c7..0000000 --- a/src/Resources/ApnsConfig.php +++ /dev/null @@ -1,98 +0,0 @@ -headers; - } - - /** - * @param array|null $headers - * @return ApnsConfig - */ - public function setHeaders(?array $headers): self - { - $this->headers = $headers; - - return $this; - } - - /** - * @return array|null - */ - public function getPayload(): ?array - { - return $this->payload; - } - - /** - * @param array|null $payload - * @return ApnsConfig - */ - public function setPayload(?array $payload): self - { - $this->payload = $payload; - - return $this; - } - - /** - * @return ApnsFcmOptions|null - */ - public function getFcmOptions(): ?ApnsFcmOptions - { - return $this->fcmOptions; - } - - /** - * @param ApnsFcmOptions $fcmOptions - * @return ApnsConfig - */ - public function setFcmOptions(ApnsFcmOptions $fcmOptions): self - { - $this->fcmOptions = $fcmOptions; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function toArray(): array - { - return [ - 'headers' => $this->getHeaders(), - 'payload' => $this->getPayload(), - 'fcm_options' => ! is_null($this->getFcmOptions()) ? $this->getFcmOptions()->toArray() : null, - ]; - } -} diff --git a/src/Resources/ApnsFcmOptions.php b/src/Resources/ApnsFcmOptions.php deleted file mode 100644 index 5e1d20d..0000000 --- a/src/Resources/ApnsFcmOptions.php +++ /dev/null @@ -1,73 +0,0 @@ -analyticsLabel; - } - - /** - * @param string|null $analyticsLabel - * @return ApnsFcmOptions - */ - public function setAnalyticsLabel(?string $analyticsLabel): self - { - $this->analyticsLabel = $analyticsLabel; - - return $this; - } - - /** - * @return string|null - */ - public function getImage(): ?string - { - return $this->image; - } - - /** - * @param string|null $image - * @return ApnsFcmOptions - */ - public function setImage(?string $image): self - { - $this->image = $image; - - return $this; - } - - /** - * @return static - */ - public static function create(): self - { - return new self; - } - - /** - * {@inheritdoc} - */ - public function toArray(): array - { - return [ - 'analytics_label' => $this->getAnalyticsLabel(), - 'image' => $this->getImage(), - ]; - } -} diff --git a/src/Resources/Color.php b/src/Resources/Color.php deleted file mode 100644 index 4b7bf34..0000000 --- a/src/Resources/Color.php +++ /dev/null @@ -1,123 +0,0 @@ -red; - } - - /** - * @param float|null $red - * @return Color - */ - public function setRed(?float $red): self - { - $this->red = $red; - - return $this; - } - - /** - * @return float|null - */ - public function getGreen(): ?float - { - return $this->green; - } - - /** - * @param float|null $green - * @return Color - */ - public function setGreen(?float $green): self - { - $this->green = $green; - - return $this; - } - - /** - * @return float|null - */ - public function getBlue(): ?float - { - return $this->blue; - } - - /** - * @param float|null $blue - * @return Color - */ - public function setBlue(?float $blue): self - { - $this->blue = $blue; - - return $this; - } - - /** - * @return float|null - */ - public function getAlpha(): ?float - { - return $this->alpha; - } - - /** - * @param float|null $alpha - * @return Color - */ - public function setAlpha(?float $alpha): self - { - $this->alpha = $alpha; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function toArray(): array - { - return [ - 'red' => $this->getRed(), - 'green' => $this->getGreen(), - 'blue' => $this->getBlue(), - 'alpha' => $this->getAlpha(), - ]; - } -} diff --git a/src/Resources/FcmOptions.php b/src/Resources/FcmOptions.php deleted file mode 100644 index 51c83c0..0000000 --- a/src/Resources/FcmOptions.php +++ /dev/null @@ -1,48 +0,0 @@ -analyticsLabel; - } - - /** - * @param string|null $analyticsLabel - * @return FcmOptions - */ - public function setAnalyticsLabel(?string $analyticsLabel): self - { - $this->analyticsLabel = $analyticsLabel; - - return $this; - } - - /** - * @return static - */ - public static function create(): self - { - return new self; - } - - /** - * {@inheritdoc} - */ - public function toArray(): array - { - return [ - 'analytics_label' => $this->getAnalyticsLabel(), - ]; - } -} diff --git a/src/Resources/FcmResource.php b/src/Resources/FcmResource.php index 51b24b2..3c9b9e5 100644 --- a/src/Resources/FcmResource.php +++ b/src/Resources/FcmResource.php @@ -2,10 +2,18 @@ namespace NotificationChannels\Fcm\Resources; -interface FcmResource +abstract class FcmResource { /** - * @return array + * @return static */ - public function toArray(): array; + public static function create(...$args): static + { + return new static(...$args); + } + + /** + * Map the resource to an array. + */ + abstract public function toArray(): array; } diff --git a/src/Resources/LightSettings.php b/src/Resources/LightSettings.php deleted file mode 100644 index 4e98418..0000000 --- a/src/Resources/LightSettings.php +++ /dev/null @@ -1,98 +0,0 @@ -color; - } - - /** - * @param Color $color - * @return LightSettings - */ - public function setColor(Color $color): self - { - $this->color = $color; - - return $this; - } - - /** - * @return string|null - */ - public function getLightOnDuration(): ?string - { - return $this->lightOnDuration; - } - - /** - * @param string|null $lightOnDuration - * @return LightSettings - */ - public function setLightOnDuration(?string $lightOnDuration): self - { - $this->lightOnDuration = $lightOnDuration; - - return $this; - } - - /** - * @return string|null - */ - public function getLightOffDuration(): ?string - { - return $this->lightOffDuration; - } - - /** - * @param string|null $lightOffDuration - * @return LightSettings - */ - public function setLightOffDuration(?string $lightOffDuration): self - { - $this->lightOffDuration = $lightOffDuration; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function toArray(): array - { - return [ - 'color' => ! is_null($this->getColor()) ? $this->getColor()->toArray() : null, - 'light_on_duration' => $this->getLightOnDuration(), - 'light_off_duration' => $this->getLightOffDuration(), - ]; - } -} diff --git a/src/Resources/Notification.php b/src/Resources/Notification.php index 6bcfe2f..60f7d73 100644 --- a/src/Resources/Notification.php +++ b/src/Resources/Notification.php @@ -2,36 +2,23 @@ namespace NotificationChannels\Fcm\Resources; -class Notification implements FcmResource +class Notification extends FcmResource { /** - * @var string|null + * Create a new notification instance. */ - protected $title; - - /** - * @var string|null - */ - protected $body; - - /** - * @var string|null - */ - protected $image; - - /** - * @return string|null - */ - public function getTitle(): ?string - { - return $this->title; + public function __construct( + public ?string $title = null, + public ?string $body = null, + public ?string $image = null + ) { + // } /** - * @param string|null $title - * @return Notification + * Set the notification title. */ - public function setTitle(?string $title): self + public function title(?string $title): self { $this->title = $title; @@ -39,18 +26,9 @@ public function setTitle(?string $title): self } /** - * @return string|null + * Set the notification body. */ - public function getBody(): ?string - { - return $this->body; - } - - /** - * @param string|null $body - * @return Notification - */ - public function setBody(?string $body): self + public function body(?string $body): self { $this->body = $body; @@ -58,18 +36,9 @@ public function setBody(?string $body): self } /** - * @return string|null - */ - public function getImage(): ?string - { - return $this->image; - } - - /** - * @param string|null $image - * @return Notification + * Set the notification image. */ - public function setImage(?string $image): self + public function image(?string $image): self { $this->image = $image; @@ -77,22 +46,14 @@ public function setImage(?string $image): self } /** - * @return static - */ - public static function create(): self - { - return new self; - } - - /** - * {@inheritdoc} + * Map the resource to an array. */ public function toArray(): array { - return [ - 'title' => $this->getTitle(), - 'body' => $this->getBody(), - 'image' => $this->getImage(), - ]; + return array_filter([ + 'title' => $this->title, + 'body' => $this->body, + 'image' => $this->image, + ]); } } diff --git a/src/Resources/NotificationPriority.php b/src/Resources/NotificationPriority.php deleted file mode 100644 index a46297a..0000000 --- a/src/Resources/NotificationPriority.php +++ /dev/null @@ -1,13 +0,0 @@ -headers; - } - - /** - * @param array|null $headers - * @return WebpushConfig - */ - public function setHeaders(?array $headers): self - { - $this->headers = $headers; - - return $this; - } - - /** - * @return array|null - */ - public function getData(): ?array - { - return $this->data; - } - - /** - * @param array|null $data - * @return WebpushConfig - * - * @throws \NotificationChannels\Fcm\Exceptions\InvalidPropertyException - */ - public function setData(?array $data): self - { - foreach ($data as $key => $item) { - if (! is_string($item)) { - throw InvalidPropertyException::mustBeString($key); - } - } - - $this->data = $data; - - return $this; - } - - /** - * @return array|null - */ - public function getNotification(): ?array - { - return $this->notification; - } - - /** - * @param array|null $notification - * @return WebpushConfig - */ - public function setNotification(?array $notification): self - { - $this->notification = $notification; - - return $this; - } - - /** - * @return WebpushFcmOptions|null - */ - public function getFcmOptions(): ?WebpushFcmOptions - { - return $this->fcmOptions; - } - - /** - * @param WebpushFcmOptions|null $fcmOptions - * @return WebpushConfig - */ - public function setFcmOptions(?WebpushFcmOptions $fcmOptions): self - { - $this->fcmOptions = $fcmOptions; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function toArray(): array - { - return [ - 'headers' => $this->getHeaders(), - 'data' => $this->getData(), - 'notification' => $this->getNotification(), - 'fcm_options' => ! is_null($this->getFcmOptions()) ? $this->getFcmOptions()->toArray() : null, - ]; - } -} diff --git a/src/Resources/WebpushFcmOptions.php b/src/Resources/WebpushFcmOptions.php deleted file mode 100644 index 1126498..0000000 --- a/src/Resources/WebpushFcmOptions.php +++ /dev/null @@ -1,73 +0,0 @@ -link; - } - - /** - * @param string|null $link - * @return WebpushFcmOptions - */ - public function setLink(?string $link): self - { - $this->link = $link; - - return $this; - } - - /** - * @return string|null - */ - public function getAnalyticsLabel(): ?string - { - return $this->analyticsLabel; - } - - /** - * @param string|null $analyticsLabel - * @return WebpushFcmOptions - */ - public function setAnalyticsLabel(?string $analyticsLabel): self - { - $this->analyticsLabel = $analyticsLabel; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function toArray(): array - { - return [ - 'link' => $this->getLink(), - 'analytics_label' => $this->getAnalyticsLabel(), - ]; - } -} diff --git a/tests/FcmMessageTest.php b/tests/FcmMessageTest.php new file mode 100644 index 0000000..a264fb5 --- /dev/null +++ b/tests/FcmMessageTest.php @@ -0,0 +1,105 @@ +assertInstanceOf(FcmMessage::class, $message); + + $this->assertEquals('name', $message->name); + } + + public function test_it_can_be_created() + { + $message = FcmMessage::create(name: 'name'); + + $this->assertInstanceOf(FcmMessage::class, $message); + + $this->assertEquals('name', $message->name); + } + + public function test_it_can_set_name() + { + $message = FcmMessage::create()->name('name'); + + $this->assertEquals(['name' => 'name'], $message->toArray()); + } + + public function test_it_can_set_token() + { + $message = FcmMessage::create()->token('token'); + + $this->assertEquals(['token' => 'token'], $message->toArray()); + } + + public function test_it_can_set_topic() + { + $message = FcmMessage::create()->topic('topic'); + + $this->assertEquals(['topic' => 'topic'], $message->toArray()); + } + + public function test_it_can_set_condition() + { + $message = FcmMessage::create()->condition('condition'); + + $this->assertEquals(['condition' => 'condition'], $message->toArray()); + } + + public function test_it_can_set_data() + { + $message = FcmMessage::create()->data(['a' => 'b']); + + $this->assertEquals(['data' => ['a' => 'b']], $message->toArray()); + } + + public function test_it_can_set_custom_attributes() + { + $message = FcmMessage::create() + ->name('name') + ->custom([ + 'notification' => [ + 'title' => 'title', + ], + ]); + + $expected = [ + 'name' => 'name', + 'notification' => [ + 'title' => 'title', + ], + ]; + + $this->assertEquals($expected, $message->toArray()); + } + + public function test_it_can_set_notification() + { + $notification = Notification::create()->title('title'); + + $message = FcmMessage::create()->notification($notification); + + $this->assertEquals([ + 'notification' => ['title' => 'title'], + ], $message->toArray()); + } + + public function test_it_can_set_client() + { + $client = Mockery::mock(Messaging::class); + + $message = FcmMessage::create()->usingClient($client); + + $this->assertSame($client, $message->client); + } +} diff --git a/tests/Resources/AndroidMessagePriorityTest.php b/tests/Resources/AndroidMessagePriorityTest.php deleted file mode 100644 index f927b34..0000000 --- a/tests/Resources/AndroidMessagePriorityTest.php +++ /dev/null @@ -1,17 +0,0 @@ -assertInstanceOf(AndroidMessagePriority::class, $instance); - } -} diff --git a/tests/Resources/NotificationPriorityTest.php b/tests/Resources/NotificationPriorityTest.php deleted file mode 100644 index a28f843..0000000 --- a/tests/Resources/NotificationPriorityTest.php +++ /dev/null @@ -1,17 +0,0 @@ -assertInstanceOf(NotificationPriority::class, $instance); - } -} diff --git a/tests/Resources/NotificationTest.php b/tests/Resources/NotificationTest.php new file mode 100644 index 0000000..237f00a --- /dev/null +++ b/tests/Resources/NotificationTest.php @@ -0,0 +1,48 @@ +assertInstanceOf(Notification::class, $message); + + $this->assertEquals('title', $message->title); + } + + public function test_it_can_be_created() + { + $message = Notification::create(title: 'title'); + + $this->assertInstanceOf(Notification::class, $message); + + $this->assertEquals('title', $message->title); + } + + public function test_it_can_set_title() + { + $message = Notification::create()->title('title'); + + $this->assertEquals(['title' => 'title'], $message->toArray()); + } + + public function test_it_can_set_body() + { + $message = Notification::create()->body('body'); + + $this->assertEquals(['body' => 'body'], $message->toArray()); + } + + public function test_it_can_set_image() + { + $message = Notification::create()->image('image'); + + $this->assertEquals(['image' => 'image'], $message->toArray()); + } +} diff --git a/tests/Resources/VisibilityTest.php b/tests/Resources/VisibilityTest.php deleted file mode 100644 index 73b8e0b..0000000 --- a/tests/Resources/VisibilityTest.php +++ /dev/null @@ -1,17 +0,0 @@ -assertInstanceOf(Visibility::class, $instance); - } -}