Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 33ec1c0

Browse files
committed
Add support for sending to external user ids
1 parent edfba7a commit 33ec1c0

File tree

5 files changed

+50
-31
lines changed

5 files changed

+50
-31
lines changed

src/Symfony/Component/Notifier/Bridge/OneSignal/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
CHANGELOG
22
=========
33

4+
7.1
5+
---
6+
* Add `OneSignalOptions::isExternalUserId()` to indicate that recipient is an external user id
7+
48
5.4
59
---
610

src/Symfony/Component/Notifier/Bridge/OneSignal/OneSignalOptions.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ public function recipient(string $id): static
108108
return $this;
109109
}
110110

111+
public function isExternalUserId(bool $flag = true): static
112+
{
113+
$this->options['is_external_user_id'] = $flag;
114+
115+
return $this;
116+
}
117+
111118
public function getRecipientId(): ?string
112119
{
113120
return $this->options['recipient_id'] ?? null;

src/Symfony/Component/Notifier/Bridge/OneSignal/OneSignalTransport.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,16 @@ protected function doSend(MessageInterface $message): SentMessage
7777

7878
$options = $options?->toArray() ?? [];
7979
$options['app_id'] = $this->appId;
80-
$options['include_player_ids'] = [$recipientId];
80+
if ($options['is_external_user_id'] ?? false) {
81+
$options['include_aliases'] = [
82+
'external_id' => [$recipientId],
83+
];
84+
$options['target_channel'] = 'push';
85+
unset($options['is_external_user_id']);
86+
} else {
87+
// include_player_ids has been deprecated since OneSignal API Version 11
88+
$options['include_subscription_ids'] = [$recipientId];
89+
}
8190
$options['headings'] ??= ['en' => $message->getSubject()];
8291
$options['contents'] ??= ['en' => $message->getContent()];
8392

src/Symfony/Component/Notifier/Bridge/OneSignal/Tests/OneSignalOptionsTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public function testOneSignalOptions()
2424
->url('https://example.com')
2525
->data(['foo' => 'bar'])
2626
->sendAfter(new \DateTimeImmutable('Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)'))
27-
->externalId('d637f30d-f709-4bed-9e2c-63637cb91894');
27+
->externalId('d637f30d-f709-4bed-9e2c-63637cb91894')
28+
->isExternalUserId();
2829

2930
$this->assertSame([
3031
'headings' => ['en' => 'English Heading', 'fr' => 'French Heading'],
@@ -33,6 +34,7 @@ public function testOneSignalOptions()
3334
'data' => ['foo' => 'bar'],
3435
'send_after' => '2015-09-24 14:00:00-0700',
3536
'external_id' => 'd637f30d-f709-4bed-9e2c-63637cb91894',
37+
'is_external_user_id' => true,
3638
], $oneSignalOptions->toArray());
3739
}
3840
}

src/Symfony/Component/Notifier/Bridge/OneSignal/Tests/OneSignalTransportTest.php

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Notifier\Bridge\OneSignal\Tests;
1313

1414
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\HttpClient\Response\JsonMockResponse;
1516
use Symfony\Component\Notifier\Bridge\OneSignal\OneSignalOptions;
1617
use Symfony\Component\Notifier\Bridge\OneSignal\OneSignalTransport;
1718
use Symfony\Component\Notifier\Exception\LogicException;
@@ -84,15 +85,7 @@ public function testSendThrowsWithoutRecipient()
8485

8586
public function testSendWithErrorResponseThrows()
8687
{
87-
$response = $this->createMock(ResponseInterface::class);
88-
$response->expects($this->exactly(2))
89-
->method('getStatusCode')
90-
->willReturn(400);
91-
$response->expects($this->once())
92-
->method('getContent')
93-
->willReturn(json_encode(['errors' => ['Message Notifications must have English language content']]));
94-
95-
$client = new MockHttpClient(static fn (): ResponseInterface => $response);
88+
$client = new MockHttpClient(new JsonMockResponse(['errors' => ['Message Notifications must have English language content']],['http_code' => 400]));
9689

9790
$transport = self::createTransport($client, 'ea345989-d273-4f21-a33b-0c006efc5edb');
9891

@@ -104,15 +97,7 @@ public function testSendWithErrorResponseThrows()
10497

10598
public function testSendWithErrorResponseThrowsWhenAllUnsubscribed()
10699
{
107-
$response = $this->createMock(ResponseInterface::class);
108-
$response->expects($this->exactly(2))
109-
->method('getStatusCode')
110-
->willReturn(200);
111-
$response->expects($this->once())
112-
->method('getContent')
113-
->willReturn(json_encode(['id' => '', 'recipients' => 0, 'errors' => ['All included players are not subscribed']]));
114-
115-
$client = new MockHttpClient(static fn (): ResponseInterface => $response);
100+
$client = new MockHttpClient(new JsonMockResponse(['id' => '', 'recipients' => 0, 'errors' => ['All included players are not subscribed']]));
116101

117102
$transport = self::createTransport($client, 'ea345989-d273-4f21-a33b-0c006efc5edb');
118103

@@ -124,20 +109,12 @@ public function testSendWithErrorResponseThrowsWhenAllUnsubscribed()
124109

125110
public function testSend()
126111
{
127-
$response = $this->createMock(ResponseInterface::class);
128-
$response->expects($this->exactly(2))
129-
->method('getStatusCode')
130-
->willReturn(200);
131-
$response->expects($this->once())
132-
->method('getContent')
133-
->willReturn(json_encode(['id' => 'b98881cc-1e94-4366-bbd9-db8f3429292b', 'recipients' => 1, 'external_id' => null]));
112+
$expectedBody = json_encode(['app_id' => '9fb175f0-0b32-4e99-ae97-bd228b9eb246', 'headings' => ['en' => 'Hello'], 'contents' => ['en' => 'World'], 'include_subscription_ids' => ['ea345989-d273-4f21-a33b-0c006efc5edb']]);
134113

135-
$expectedBody = json_encode(['app_id' => '9fb175f0-0b32-4e99-ae97-bd228b9eb246', 'headings' => ['en' => 'Hello'], 'contents' => ['en' => 'World'], 'include_player_ids' => ['ea345989-d273-4f21-a33b-0c006efc5edb']]);
136-
137-
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface {
114+
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($expectedBody): ResponseInterface {
138115
$this->assertJsonStringEqualsJsonString($expectedBody, $options['body']);
139116

140-
return $response;
117+
return new JsonMockResponse(['id' => 'b98881cc-1e94-4366-bbd9-db8f3429292b', 'recipients' => 1, 'external_id' => null]);
141118
});
142119

143120
$transport = self::createTransport($client, 'ea345989-d273-4f21-a33b-0c006efc5edb');
@@ -146,4 +123,24 @@ public function testSend()
146123

147124
$this->assertSame('b98881cc-1e94-4366-bbd9-db8f3429292b', $sentMessage->getMessageId());
148125
}
126+
127+
public function testSendExternalIds()
128+
{
129+
$expectedBody = json_encode(['app_id' => '9fb175f0-0b32-4e99-ae97-bd228b9eb246', 'headings' => ['en' => 'Hello'], 'contents' => ['en' => 'World'], 'include_aliases' => ['external_id' => ['ea345989-d273-4f21-a33b-0c006efc5edb']], 'target_channel' => 'push']);
130+
131+
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($expectedBody): ResponseInterface {
132+
$this->assertJsonStringEqualsJsonString($expectedBody, $options['body']);
133+
134+
return new JsonMockResponse(['id' => 'b98881cc-1e94-4366-bbd9-db8f3429292b', 'recipients' => 1, 'external_id' => null]);
135+
});
136+
137+
$transport = self::createTransport($client, 'ea345989-d273-4f21-a33b-0c006efc5edb');
138+
139+
$options = new OneSignalOptions();
140+
$options->isExternalUserId();
141+
142+
$sentMessage = $transport->send(new PushMessage('Hello', 'World', $options));
143+
144+
$this->assertSame('b98881cc-1e94-4366-bbd9-db8f3429292b', $sentMessage->getMessageId());
145+
}
149146
}

0 commit comments

Comments
 (0)