Skip to content

Commit

Permalink
Add changeable appId feature (#3)
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

Co-authored-by: Enver Cigal <[email protected]>
  • Loading branch information
envercigal and Enver Cigal committed Mar 2, 2022
1 parent cfbf223 commit 8944571
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ class AccountApproved extends Notification
}
```

You can change appId of a specific notification, just add the setAppId() method

```php
public function toOneSignal($notifiable)
{
return OneSignalMessage::create()
->setAppId('Other AppId')
}
```

In your notifiable model, make sure to include a routeNotificationForOneSignal() method.

```php
Expand Down
2 changes: 1 addition & 1 deletion src/OneSignalChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function send($notifiable, Notification $notification): ?object
$result = Http::timeout($this->timeout)
->asJson()->acceptJson()
->post(self::ENDPOINT, [
'app_id' => $this->appId,
'app_id' => $message->getAppId() ?? $this->appId,
'headings' => $message->getHeadings(),
'contents' => $message->getBody(),
'data' => $message->getData(),
Expand Down
18 changes: 18 additions & 0 deletions src/OneSignalMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class OneSignalMessage
{
private ?string $appId = null;

private array $contents;

private array $headings;
Expand Down Expand Up @@ -71,6 +73,22 @@ public function setData(array $value): self
return $this;
}

/**
* @param string $appId
* @return $this
*/
public function setAppId(string $appId): self
{
$this->appId = $appId;

return $this;
}

public function getAppId(): ?string
{
return $this->appId;
}

public function getData(): ?array
{
return $this->data;
Expand Down
19 changes: 19 additions & 0 deletions tests/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Illuminate\Support\Facades\Http;
use Macellan\OneSignal\Exceptions\CouldNotSendNotification;
use Macellan\OneSignal\OneSignalChannel;
use Macellan\OneSignal\Tests\Notifications\TestOtherAppIdNotification;
use Macellan\OneSignal\Tests\Notifications\TestNotification;

class ChannelTest extends TestCase
{
Expand Down Expand Up @@ -62,4 +64,21 @@ public function test_not_success_notification()

(new Notifiable)->notify(new TestNotification());
}

public function test_change_app_id()
{
Http::fake([
'api/v1/notifications' => Http::response([
"id" => "931082f5-e442-42b1-a951-19e7e45dee39",
"recipients" => 1,
"external_id" => null,
]),
]);

(new Notifiable)->notify(new TestOtherAppIdNotification());

Http::assertSent(function (Request $request) {
return $request['app_id'] == 'other_app_id';
});
}
}
12 changes: 12 additions & 0 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ public function test_create_multiple_lang()
$this->assertEquals($subject, $message->getHeadings());
$this->assertEquals($data, $message->getData());
}

public function test_change_app_id()
{
$body = 'body';

$appId = 'other_app_id';

$message = OneSignalMessage::create($body)->setAppId($appId);

$this->assertEquals(['en' => $body], $message->getBody());
$this->assertEquals($appId, $message->getAppId());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Macellan\OneSignal\Tests;
namespace Macellan\OneSignal\Tests\Notifications;

use Illuminate\Notifications\Notification;
use Macellan\OneSignal\OneSignalMessage;
Expand Down
22 changes: 22 additions & 0 deletions tests/Notifications/TestOtherAppIdNotification.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 TestOtherAppIdNotification extends Notification
{
public function via()
{
return ['onesignal'];
}

public function toOneSignal($notifiable)
{
return OneSignalMessage::create()
->setAppId('other_app_id')
->setSubject('Subject')
->setBody('Body');
}
}
4 changes: 3 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ protected function setUp(): void
{
parent::setUp();

config(['services.onesignal.app_id' => $this->appId]);
config([
'services.onesignal.app_id' => $this->appId,
]);
}

protected function getPackageProviders($app)
Expand Down

0 comments on commit 8944571

Please sign in to comment.