From 3040d9db0d603c463eabdc885ea56ce89e5f80db Mon Sep 17 00:00:00 2001 From: Dwight Watson Date: Fri, 13 Oct 2023 13:16:32 +1100 Subject: [PATCH] Restore Notification resource --- README.md | 12 +++--- src/FcmMessage.php | 19 ++++++++- src/Resources/FcmResource.php | 19 +++++++++ src/Resources/Notification.php | 63 ++++++++++++++++++++++++++++ tests/FcmMessageTest.php | 12 ++++++ tests/Resources/NotificationTest.php | 37 ++++++++++++++++ 6 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 src/Resources/FcmResource.php create mode 100644 src/Resources/Notification.php create mode 100644 tests/Resources/NotificationTest.php diff --git a/README.md b/README.md index a0b1022..2ac9084 100644 --- a/README.md +++ b/README.md @@ -50,6 +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\Notification as FcmNotification; class AccountActivated extends Notification { @@ -61,13 +62,14 @@ class AccountActivated extends Notification public function toFcm($notifiable) { return FcmMessage::create() + ->notification( + FcmNotification::create() + ->title('Account Activated') + ->body('Your account has been activated.') + ->image('http://example.com/url-to-image-here.png') + ) ->setData(['data1' => 'value', 'data2' => 'value2']) ->custom([ - 'notification' => [ - 'title' => 'Account Activated', - 'body' => 'Your account has been activated', - 'image' => 'http://example.com/url-to-image-here.png', - ], 'android' => [ 'notification' => [ 'color' => '#0A0A0A', diff --git a/src/FcmMessage.php b/src/FcmMessage.php index d237a2f..9de54ad 100644 --- a/src/FcmMessage.php +++ b/src/FcmMessage.php @@ -2,6 +2,7 @@ namespace NotificationChannels\Fcm; +use NotificationChannels\Fcm\Resources\Notification; use Illuminate\Support\Traits\Macroable; use Kreait\Firebase\Contract\Messaging; use Kreait\Firebase\Messaging\Message; @@ -40,10 +41,15 @@ class FcmMessage implements Message */ public array $custom = []; + /** + * The message notification. + */ + public ?Notification $notification = null; + /** * The custom messaging client. */ - public ?Messaging $client; + public ?Messaging $client = null; /** * Create a new message instance. @@ -113,6 +119,16 @@ public function custom(?array $custom): self return $this; } + /** + * Set the message notification. + */ + public function notification(Notification $notification): self + { + $this->notification = $notification; + + return $this; + } + /** * Set the message Firebase Messaging client instance. */ @@ -131,6 +147,7 @@ public function toArray() 'token' => $this->token, 'topic' => $this->topic, 'condition' => $this->condition, + 'notification' => $this->notification?->toArray(), ...$this->custom, ]); } diff --git a/src/Resources/FcmResource.php b/src/Resources/FcmResource.php new file mode 100644 index 0000000..b61ea9d --- /dev/null +++ b/src/Resources/FcmResource.php @@ -0,0 +1,19 @@ +title = $title; + + return $this; + } + + /** + * Set the notification body. + */ + public function body(?string $body): self + { + $this->body = $body; + + return $this; + } + + /** + * Set the notification image. + */ + public function image(?string $image): self + { + $this->image = $image; + + return $this; + } + + /** + * Map the resource to an array. + */ + public function toArray(): array + { + return array_filter([ + 'title' => $this->title, + 'body' => $this->body, + 'image' => $this->image, + ]); + } +} \ No newline at end of file diff --git a/tests/FcmMessageTest.php b/tests/FcmMessageTest.php index b05e2ec..d050b47 100644 --- a/tests/FcmMessageTest.php +++ b/tests/FcmMessageTest.php @@ -5,6 +5,7 @@ use Kreait\Firebase\Contract\Messaging; use Mockery; use NotificationChannels\Fcm\FcmMessage; +use NotificationChannels\Fcm\Resources\Notification; use PHPUnit\Framework\TestCase; class FcmMessageTest extends TestCase @@ -71,6 +72,17 @@ public function test_it_can_set_custom_attributes() $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); diff --git a/tests/Resources/NotificationTest.php b/tests/Resources/NotificationTest.php new file mode 100644 index 0000000..0ef0ded --- /dev/null +++ b/tests/Resources/NotificationTest.php @@ -0,0 +1,37 @@ +assertInstanceOf(Notification::class, $message); + } + + 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()); + } +}