Skip to content

Commit d3edebf

Browse files
committed
Add Expo Notifier
1 parent 810599d commit d3edebf

17 files changed

+517
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+2
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
115115
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
116116
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
117+
use Symfony\Component\Notifier\Bridge\Expo\ExpoTransportFactory;
117118
use Symfony\Component\Notifier\Bridge\FakeChat\FakeChatTransportFactory;
118119
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory;
119120
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
@@ -2432,6 +2433,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24322433
ClickatellTransportFactory::class => 'notifier.transport_factory.clickatell',
24332434
DiscordTransportFactory::class => 'notifier.transport_factory.discord',
24342435
EsendexTransportFactory::class => 'notifier.transport_factory.esendex',
2436+
ExpoTransportFactory::class => 'notifier.transport_factory.expo',
24352437
FakeChatTransportFactory::class => 'notifier.transport_factory.fakechat',
24362438
FakeSmsTransportFactory::class => 'notifier.transport_factory.fakesms',
24372439
FirebaseTransportFactory::class => 'notifier.transport_factory.firebase',

src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
1717
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
1818
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
19+
use Symfony\Component\Notifier\Bridge\Expo\ExpoTransportFactory;
1920
use Symfony\Component\Notifier\Bridge\FakeChat\FakeChatTransportFactory;
2021
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory;
2122
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
@@ -225,5 +226,9 @@
225226
->set('notifier.transport_factory.turbosms', TurboSmsTransportFactory::class)
226227
->parent('notifier.transport_factory.abstract')
227228
->tag('texter.transport_factory')
229+
230+
->set('notifier.transport_factory.expo', ExpoTransportFactory::class)
231+
->parent('notifier.transport_factory.abstract')
232+
->tag('chatter.transport_factory')
228233
;
229234
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.3
5+
---
6+
7+
* Add the bridge
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Expo;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author Imad ZAIRIG <https://github.com/zairigimad>
18+
*
19+
* @see https://docs.expo.dev/push-notifications/sending-notifications/
20+
*
21+
* @experimental in 5.4
22+
*/
23+
final class ExpoOptions implements MessageOptionsInterface
24+
{
25+
private $to;
26+
27+
/**
28+
* @see https://docs.expo.dev/push-notifications/sending-notifications/#message-request-format
29+
*/
30+
protected $options;
31+
32+
private $data;
33+
34+
public function __construct(string $to, array $options, array $data = [])
35+
{
36+
$this->to = $to;
37+
$this->options = $options;
38+
$this->data = $data;
39+
}
40+
41+
public function toArray(): array
42+
{
43+
return [
44+
'to' => $this->to,
45+
'title' => $this->options['title'],
46+
'body' => $this->options['body'],
47+
'data' => $this->data,
48+
];
49+
}
50+
51+
public function getRecipientId(): ?string
52+
{
53+
return $this->to;
54+
}
55+
56+
public function title(string $title): self
57+
{
58+
$this->options['title'] = $title;
59+
60+
return $this;
61+
}
62+
63+
public function subtitle(string $subtitle): self
64+
{
65+
$this->options['subtitle'] = $subtitle;
66+
67+
return $this;
68+
}
69+
70+
public function priority(string $priority): self
71+
{
72+
$this->options['priority'] = $priority;
73+
74+
return $this;
75+
}
76+
77+
public function sound(string $sound): self
78+
{
79+
$this->options['sound'] = $sound;
80+
81+
return $this;
82+
}
83+
84+
public function badge(int $badge): self
85+
{
86+
$this->options['badge'] = $badge;
87+
88+
return $this;
89+
}
90+
91+
public function channelId(string $channelId): self
92+
{
93+
$this->options['channelId'] = $channelId;
94+
95+
return $this;
96+
}
97+
98+
public function categoryId(string $categoryId): self
99+
{
100+
$this->options['categoryId'] = $categoryId;
101+
102+
return $this;
103+
}
104+
105+
public function mutableContent(bool $mutableContent): self
106+
{
107+
$this->options['mutableContent'] = $mutableContent;
108+
109+
return $this;
110+
}
111+
112+
public function body(string $body): self
113+
{
114+
$this->options['body'] = $body;
115+
116+
return $this;
117+
}
118+
119+
public function ttl(int $ttl): self
120+
{
121+
$this->options['ttl'] = $ttl;
122+
123+
return $this;
124+
}
125+
126+
public function expiration(int $expiration): self
127+
{
128+
$this->options['expiration'] = $expiration;
129+
130+
return $this;
131+
}
132+
133+
public function data(array $data): self
134+
{
135+
$this->data = $data;
136+
137+
return $this;
138+
}
139+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Expo;
13+
14+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
15+
use Symfony\Component\Notifier\Exception\TransportException;
16+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
17+
use Symfony\Component\Notifier\Message\ChatMessage;
18+
use Symfony\Component\Notifier\Message\MessageInterface;
19+
use Symfony\Component\Notifier\Message\SentMessage;
20+
use Symfony\Component\Notifier\Transport\AbstractTransport;
21+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
22+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
23+
use Symfony\Contracts\HttpClient\HttpClientInterface;
24+
25+
/**
26+
* @author Imad ZAIRIG <https://github.com/zairigimad>
27+
*
28+
* @experimental in 5.4
29+
*/
30+
final class ExpoTransport extends AbstractTransport
31+
{
32+
protected const HOST = 'exp.host/--/api/v2/push/send';
33+
34+
/** @var string */
35+
private $token;
36+
37+
public function __construct(string $token = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
38+
{
39+
$this->token = $token;
40+
$this->client = $client;
41+
42+
parent::__construct($client, $dispatcher);
43+
}
44+
45+
public function __toString(): string
46+
{
47+
return sprintf('expo://%s', $this->getEndpoint());
48+
}
49+
50+
public function supports(MessageInterface $message): bool
51+
{
52+
return $message instanceof ChatMessage;
53+
}
54+
55+
protected function doSend(MessageInterface $message): SentMessage
56+
{
57+
if (!$message instanceof ChatMessage) {
58+
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
59+
}
60+
61+
$endpoint = sprintf('https://%s', $this->getEndpoint());
62+
$options = ($opts = $message->getOptions()) ? $opts->toArray() : [];
63+
if (!isset($options['to'])) {
64+
$options['to'] = $message->getRecipientId();
65+
}
66+
if (null === $options['to']) {
67+
throw new InvalidArgumentException(sprintf('The "%s" transport required the "to" option to be set.', __CLASS__));
68+
}
69+
$options['body'] = $message->getSubject();
70+
$options['data'] = $options['data'] ?? [];
71+
72+
$response = $this->client->request('POST', $endpoint, [
73+
'headers' => [
74+
'Authorization' => $this->token ? sprintf('Bearer %s', $this->token) : null,
75+
],
76+
'json' => array_filter($options),
77+
]);
78+
79+
try {
80+
$statusCode = $response->getStatusCode();
81+
} catch (TransportExceptionInterface $e) {
82+
throw new TransportException('Could not reach the remote Expo server.', $response, 0, $e);
83+
}
84+
85+
$contentType = $response->getHeaders(false)['content-type'][0] ?? '';
86+
$jsonContents = 0 === strpos($contentType, 'application/json') ? $response->toArray(false) : null;
87+
88+
if (200 !== $statusCode) {
89+
$errorMessage = $jsonContents ? $jsonContents['error']['message'] : $response->getContent(false);
90+
91+
throw new TransportException('Unable to post the Expo message: '.$errorMessage, $response);
92+
}
93+
if ($jsonContents && isset($jsonContents['error']['message'])) {
94+
throw new TransportException('Unable to post the Expo message: '.$jsonContents['errors'], $response);
95+
}
96+
97+
$success = $response->toArray(false);
98+
99+
$sentMessage = new SentMessage($message, (string) $this);
100+
$sentMessage->setMessageId($success['data']['id']);
101+
102+
return $sentMessage;
103+
}
104+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Expo;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
use Symfony\Component\Notifier\Transport\TransportInterface;
18+
19+
/**
20+
* @author Imad ZAIRIG <https://github.com/zairigimad>
21+
*
22+
* @experimental in 5.4
23+
*/
24+
final class ExpoTransportFactory extends AbstractTransportFactory
25+
{
26+
public function create(Dsn $dsn): TransportInterface
27+
{
28+
$scheme = $dsn->getScheme();
29+
30+
if ('expo' !== $scheme) {
31+
throw new UnsupportedSchemeException($dsn, 'expo', $this->getSupportedSchemes());
32+
}
33+
34+
$token = $dsn->getOption('accessToken');
35+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
36+
$port = $dsn->getPort();
37+
38+
return (new ExpoTransport($token, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
39+
}
40+
41+
protected function getSupportedSchemes(): array
42+
{
43+
return ['expo'];
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2021 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Expo Notifier
2+
=================
3+
4+
Provides [Expo](https://docs.expo.dev/versions/latest/sdk/notifications/) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
EXPO_DSN=expo://default?accessToken=TOKEN
11+
```
12+
13+
where:
14+
- `TOKEN` is your Expo Access Token
15+
16+
Resources
17+
---------
18+
19+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
20+
* [Report issues](https://github.com/symfony/symfony/issues) and
21+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
22+
in the [main Symfony repository](https://github.com/symfony/symfony)

0 commit comments

Comments
 (0)