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

Skip to content

Commit 366baef

Browse files
committed
feature #53262 [Notifier] [OneSignal] Add support for sending to external user ids (KDederichs)
This PR was merged into the 7.1 branch. Discussion ---------- [Notifier] [OneSignal] Add support for sending to external user ids | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | Fix #50779 | License | MIT As discussed in #53248, here's the feature PR against 7.1. This introduces a new `isExternalUserId()` option to indicate that the receiver is an external user id. At the same time it also replaces the deprecated `include_player_ids` option. Commits ------- 533a831 Add support for sending to external user ids
2 parents fe482a4 + 533a831 commit 366baef

File tree

5 files changed

+61
-31
lines changed

5 files changed

+61
-31
lines changed

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

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

4+
7.1
5+
---
6+
7+
* Add `OneSignalOptions::isExternalUserId()` to indicate that recipient is an external user id
8+
49
5.4
510
---
611

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

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

111+
/**
112+
* Indicates that the passed recipient is an external user id.
113+
*
114+
* For more information on how to set an external user id in OneSignal please see:
115+
* https://documentation.onesignal.com/docs/aliases-external-id
116+
*
117+
* For more information on how targeting based on external user id works please see:
118+
* https://documentation.onesignal.com/reference/create-notification
119+
*
120+
* @return $this
121+
*/
122+
public function isExternalUserId(bool $flag = true): static
123+
{
124+
$this->options['is_external_user_id'] = $flag;
125+
126+
return $this;
127+
}
128+
111129
public function getRecipientId(): ?string
112130
{
113131
return $this->options['recipient_id'] ?? null;

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,15 @@ 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+
$options['include_subscription_ids'] = [$recipientId];
88+
}
8189
$options['headings'] ??= ['en' => $message->getSubject()];
8290
$options['contents'] ??= ['en' => $message->getContent()];
8391

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)