Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 854617f

Browse filesBrowse files
committed
Add Expo Notifier
1 parent 810599d commit 854617f
Copy full SHA for 854617f

16 files changed

+525
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+2Lines changed: 2 additions & 0 deletions
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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php
+5Lines changed: 5 additions & 0 deletions
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
};
+4Lines changed: 4 additions & 0 deletions
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
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.3
5+
---
6+
7+
* Added the bridge
+148Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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.3
22+
*/
23+
abstract 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+
'ttl' => $this->options['ttl'],
48+
'subtitle' => $this->options['subtitle'],
49+
'priority' => $this->options['priority'],
50+
'sound' => $this->options['sound'],
51+
'badge' => $this->options['badge'],
52+
'channelId' => $this->options['channelId'],
53+
'categoryId' => $this->options['categoryId'],
54+
'mutableContent' => $this->options['mutableContent'],
55+
'expiration' => $this->options['expiration'],
56+
'data' => $this->data,
57+
];
58+
}
59+
60+
public function getRecipientId(): ?string
61+
{
62+
return $this->to;
63+
}
64+
65+
public function title(string $title): self
66+
{
67+
$this->options['title'] = $title;
68+
69+
return $this;
70+
}
71+
72+
public function subtitle(string $subtitle): self
73+
{
74+
$this->options['subtitle'] = $subtitle;
75+
76+
return $this;
77+
}
78+
79+
public function priority(string $priority): self
80+
{
81+
$this->options['priority'] = $priority;
82+
83+
return $this;
84+
}
85+
86+
public function sound(string $sound): self
87+
{
88+
$this->options['sound'] = $sound;
89+
90+
return $this;
91+
}
92+
93+
public function badge(int $badge): self
94+
{
95+
$this->options['badge'] = $badge;
96+
97+
return $this;
98+
}
99+
100+
public function channelId(string $channelId): self
101+
{
102+
$this->options['channelId'] = $channelId;
103+
104+
return $this;
105+
}
106+
107+
public function categoryId(string $categoryId): self
108+
{
109+
$this->options['categoryId'] = $categoryId;
110+
111+
return $this;
112+
}
113+
114+
public function mutableContent(bool $mutableContent): self
115+
{
116+
$this->options['mutableContent'] = $mutableContent;
117+
118+
return $this;
119+
}
120+
121+
public function body(string $body): self
122+
{
123+
$this->options['body'] = $body;
124+
125+
return $this;
126+
}
127+
128+
public function ttl(int $ttl): self
129+
{
130+
$this->options['ttl'] = $ttl;
131+
132+
return $this;
133+
}
134+
135+
public function expiration(int $expiration): self
136+
{
137+
$this->options['expiration'] = $expiration;
138+
139+
return $this;
140+
}
141+
142+
public function data(array $data): self
143+
{
144+
$this->data = $data;
145+
146+
return $this;
147+
}
148+
}
+104Lines changed: 104 additions & 0 deletions
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.3
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, 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' => sprintf('Bearer %s', $this->token),
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['errors']['message'] : $response->getContent(false);
90+
91+
throw new TransportException('Unable to post the Expo message: '.$errorMessage, $response);
92+
}
93+
if ($jsonContents && isset($jsonContents['errors']['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'][0]['id']);
101+
102+
return $sentMessage;
103+
}
104+
}
+45Lines changed: 45 additions & 0 deletions
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.3
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+
}
+19Lines changed: 19 additions & 0 deletions
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.
+22Lines changed: 22 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.