Skip to content

Commit

Permalink
Add icon feature (#7)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
envercigal and Enver Cigal authored Sep 2, 2022
1 parent 9f33380 commit 5363641
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/OneSignalChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
14 changes: 14 additions & 0 deletions src/OneSignalMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class OneSignalMessage

private ?array $data = null;

private ?string $icon = null;

public static function create($body = ''): self
{
return new static($body);
Expand Down Expand Up @@ -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;
Expand All @@ -100,4 +109,9 @@ public function getBody(): array
{
return $this->contents;
}

public function getIcon(): ?string
{
return $this->icon;
}
}
20 changes: 20 additions & 0 deletions tests/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'];
});
}
}
8 changes: 7 additions & 1 deletion tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
22 changes: 22 additions & 0 deletions tests/Notifications/TestIconNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Macellan\OneSignal\Tests\Notifications;

use Illuminate\Notifications\Notification;
use Macellan\OneSignal\OneSignalMessage;

class TestIconNotification extends Notification
{
public function via()
{
return ['onesignal'];
}

public function toOneSignal()
{
return OneSignalMessage::create()
->setSubject('Subject')
->setBody('Body')
->setIcon('test-icon.jpg');
}
}
2 changes: 1 addition & 1 deletion tests/Notifications/TestNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function via()
return ['onesignal'];
}

public function toOneSignal($notifiable)
public function toOneSignal()
{
return OneSignalMessage::create()
->setSubject('Subject')
Expand Down
2 changes: 1 addition & 1 deletion tests/Notifications/TestOtherAppIdNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function via()
return ['onesignal'];
}

public function toOneSignal($notifiable)
public function toOneSignal()
{
return OneSignalMessage::create()
->setAppId('other_app_id')
Expand Down

0 comments on commit 5363641

Please sign in to comment.