From 5363641dde4df9c4c782d9c5bf2809378462d9d9 Mon Sep 17 00:00:00 2001 From: envercigal <31448593+envercigal@users.noreply.github.com> Date: Fri, 2 Sep 2022 19:44:35 +0300 Subject: [PATCH] Add icon feature (#7) * Add changeable appId feature * Add set app id feature for message * Add change app id test * Update readme * Code refactoring * Add icon feature Co-authored-by: Enver Cigal --- src/OneSignalChannel.php | 4 ++++ src/OneSignalMessage.php | 14 ++++++++++++ tests/ChannelTest.php | 20 +++++++++++++++++ tests/MessageTest.php | 8 ++++++- tests/Notifications/TestIconNotification.php | 22 +++++++++++++++++++ tests/Notifications/TestNotification.php | 2 +- .../TestOtherAppIdNotification.php | 2 +- 7 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 tests/Notifications/TestIconNotification.php diff --git a/src/OneSignalChannel.php b/src/OneSignalChannel.php index 2d7f470..b1c3ace 100644 --- a/src/OneSignalChannel.php +++ b/src/OneSignalChannel.php @@ -50,7 +50,11 @@ public function send($notifiable, Notification $notification): ?object 'headings' => $message->getHeadings(), 'contents' => $message->getBody(), 'data' => $message->getData(), + 'large_icon' => $message->getIcon(), + 'huawei_large_icon' => $message->getIcon(), + 'ios_attachments' => ['icon' => $message->getIcon()], 'include_player_ids' => is_array($userIds) ? $userIds : [$userIds], + ]); if ($requestException = $result->toException()) { diff --git a/src/OneSignalMessage.php b/src/OneSignalMessage.php index 0f2471e..3fb8fcb 100644 --- a/src/OneSignalMessage.php +++ b/src/OneSignalMessage.php @@ -12,6 +12,8 @@ class OneSignalMessage private ?array $data = null; + private ?string $icon = null; + public static function create($body = ''): self { return new static($body); @@ -81,6 +83,13 @@ public function setAppId(string $appId): self return $this; } + public function setIcon(string $icon): self + { + $this->icon = $icon; + + return $this; + } + public function getAppId(): ?string { return $this->appId; @@ -100,4 +109,9 @@ public function getBody(): array { return $this->contents; } + + public function getIcon(): ?string + { + return $this->icon; + } } diff --git a/tests/ChannelTest.php b/tests/ChannelTest.php index f4a2658..228e157 100644 --- a/tests/ChannelTest.php +++ b/tests/ChannelTest.php @@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Http; use Macellan\OneSignal\Exceptions\CouldNotSendNotification; use Macellan\OneSignal\OneSignalChannel; +use Macellan\OneSignal\Tests\Notifications\TestIconNotification; use Macellan\OneSignal\Tests\Notifications\TestNotification; use Macellan\OneSignal\Tests\Notifications\TestOtherAppIdNotification; @@ -81,4 +82,23 @@ public function test_change_app_id() return $request['app_id'] == 'other_app_id'; }); } + + public function test_icon() + { + Http::fake([ + 'api/v1/notifications' => Http::response([ + 'id' => '931082f5-e442-42b1-a951-19e7e45dee39', + 'recipients' => 1, + 'external_id' => null, + ]), + ]); + + (new Notifiable)->notify(new TestIconNotification()); + + Http::assertSent(function (Request $request) { + return $request['huawei_large_icon'] == 'test-icon.jpg' && + $request['large_icon'] == 'test-icon.jpg' && + $request['ios_attachments'] == ['icon' => 'test-icon.jpg']; + }); + } } diff --git a/tests/MessageTest.php b/tests/MessageTest.php index a66f4e1..628907d 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -17,11 +17,17 @@ public function test_create() 'tr' => 'Data', ]; - $message = OneSignalMessage::create($body)->setSubject($subject)->setData($data); + $icon = 'test_icon.png'; + + $message = OneSignalMessage::create($body) + ->setSubject($subject) + ->setData($data) + ->setIcon($icon); $this->assertEquals(['en' => $body], $message->getBody()); $this->assertEquals(['en' => $subject], $message->getHeadings()); $this->assertEquals($data, $message->getData()); + $this->assertEquals($icon, $message->getIcon()); } public function test_create_multiple_lang() diff --git a/tests/Notifications/TestIconNotification.php b/tests/Notifications/TestIconNotification.php new file mode 100644 index 0000000..cb992b5 --- /dev/null +++ b/tests/Notifications/TestIconNotification.php @@ -0,0 +1,22 @@ +setSubject('Subject') + ->setBody('Body') + ->setIcon('test-icon.jpg'); + } +} diff --git a/tests/Notifications/TestNotification.php b/tests/Notifications/TestNotification.php index 9b71dd9..bd6c4df 100644 --- a/tests/Notifications/TestNotification.php +++ b/tests/Notifications/TestNotification.php @@ -12,7 +12,7 @@ public function via() return ['onesignal']; } - public function toOneSignal($notifiable) + public function toOneSignal() { return OneSignalMessage::create() ->setSubject('Subject') diff --git a/tests/Notifications/TestOtherAppIdNotification.php b/tests/Notifications/TestOtherAppIdNotification.php index 7176d6c..d97e044 100644 --- a/tests/Notifications/TestOtherAppIdNotification.php +++ b/tests/Notifications/TestOtherAppIdNotification.php @@ -12,7 +12,7 @@ public function via() return ['onesignal']; } - public function toOneSignal($notifiable) + public function toOneSignal() { return OneSignalMessage::create() ->setAppId('other_app_id')