8000 feature #42414 [Notifier] Add Expo bridge (zairigimad) · Korbeil/symfony@e399e9d · GitHub
[go: up one dir, main page]

Skip to content

Commit e399e9d

Browse files
committed
feature symfony#42414 [Notifier] Add Expo bridge (zairigimad)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Notifier] Add Expo bridge | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | no ticket | License | MIT Expo makes implementing push notifications almost too easy. All the hassle with native device information and communicating with APNs (Apple Push Notification service) or FCM (Firebase Cloud Messaging) is taken care of behind the scenes, so that you can treat iOS and Android notifications the same, saving you time on the front-end, and back-end! this PR will add the support of [Expo Notification](https://docs.expo.dev/push-notifications/overview/) as a bridge to Symfony screenshot from a real application build with expo ![2DA98E58-4F0A-4A89-B6CB-937878E00E4A](https://user-images.githubusercontent.com/9056839/128602022-4dde3a56-f623-49d0-8b66-7b1d1414169c.jpeg) ✏️ this is a work in progress I would love to hear your feedbacks to improve it. Commits ------- 7d83508 [Notifier] Add Expo bridge
2 parents 4202f2a + 7d83508 commit e399e9d

File tree

16 files changed

+549
-0
lines changed

16 files changed

+549
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
118118
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
119119
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
120+
use Symfony\Component\Notifier\Bridge\Expo\ExpoTransportFactory;
120121
use Symfony\Component\Notifier\Bridge\FakeChat\FakeChatTransportFactory;
121122
use Symfony\Component\Notifier\Bridge\FakeSms\FakeSmsTransportFactory;
122123
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
@@ -2464,6 +2465,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24642465
ClickatellTransportFactory::class => 'notifier.transport_factory.clickatell',
24652466
DiscordTransportFactory::class => 'notifier.transport_factory.discord',
24662467
EsendexTransportFactory::class => 'notifier.transport_factory.esendex',
2468+
ExpoTransportFactory::class => 'notifier.transport_factory.expo',
24672469
FakeChatTransportFactory::class => 'notifier.transport_factory.fakechat',
24682470
FakeSmsTransportFactory::class => 'notifier.transport_factory.fakesms',
24692471
FirebaseTransportFactory::class => 'notifier.transport_factory.firebase',

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

Lines changed: 5 additions & 0 deletions
< 8000 td data-grid-cell-id="diff-da5ba601ccf4ea3e0a2c64da052e48011d2e01195502fd77bc64d3fd7e79de8a-237-239-1" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">239
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;
@@ -235,5 +236,9 @@
235236
->set('notifier.transport_factory.onesignal', OneSignalTransportFactory::class)
236237
->parent('notifier.transport_factory.abstract')
237238
->tag('texter.transport_factory')
+
240+
->set('notifier.transport_factory.expo', ExpoTransportFactory::class)
241+
->parent('notifier.transport_factory.abstract')
242+
->tag('chatter.transport_factory')
238243
;
239244
};
Lines 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
Lines 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
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.4
5+
---
6+
7+
* Add the bridge
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
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+
* 8000 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+
final class ExpoOptions implements MessageOptionsInterface
22+
{
23+
private $to;
24+
25+
/**
26+
* @see https://docs.expo.dev/push-notifications/sending-notifications/#message-request-format
27+
*/
28+
protected $options;
29+
30+
private $data;
31+
32+
public function __construct(string $to, array $options = [], array $data = [])
33+
{
34+
$this->to = $to;
35+
$this->options = $options;
36+
$this->data = $data;
37+
}
38+
39+
public function toArray(): array
40+
{
41+
return array_merge(
42+
$this->options,
43+
[
44+
'to' => $this->to,
45+
'data' => $this->data,
46+
]
47+
);
48+
}
49+
50+
public function getRecipientId(): ?string
51+
{
52+
return $this->to;
53+
}
54+
55+
/**
56+
* @return $this
57+
*/
58+
public function title(string $title): self
59+
{
60+
$this->options['title'] = $title;
61+
62+
return $this;
63+
}
64+
65+
/**
66+
* @return $this
67+
*/
68+
public function subtitle(string $subtitle): self
69+
{
70+
$this->options['subtitle'] = $subtitle;
71+
72+
return $this;
73+
}
74+
75+
/**
76+
* @return $this
77+
*/
78+
public function priority(string $priority): self
79+
{
80+
$this->options['priority'] = $priority;
81+
82+
return $this;
83+
}
84+
85+
/**
86+
* @return $this
87+
*/
88+
public function sound(string $sound): self
89+
{
90+
$this->options['sound'] = $sound;
91+
92+
return $this;
93+
}
94+
95+
/**
96+
* @return $this
97+
*/
98+
public function badge(int $badge): self
99+
{
100+
$this->options['badge'] = $badge;
101+
102+
return $this;
103+
}
104+
105+
/**
106+
* @return $this
107+
*/
108+
public function channelId(string $channelId): self
109+
{
110+
$this->options['channelId'] = $channelId;
111+
112+
return $this;
113+
}
114+
115+
/**
116+
* @return $this
117+
*/
118+
public function categoryId(string $categoryId): self
119+
{
120+
$this->options['categoryId'] = $categoryId;
121+
122+
return $this;
123+
}
124+
125+
/**
126+
* @return $this
127+
*/
128+
public function mutableContent(bool $mutableContent): self
129+
{
130+
$this->options['mutableContent'] = $mutableContent;
131+
132+
return $this;
133+
}
134+
135+
/**
136+
* @return $this
137+
*/
138+
public function body(string $body): self
139+
{
140+
$this->options['body'] = $body;
141+
142+
return $this;
143+
}
144+
145+
/**
146+
* @return $this
147+
*/
148+
public function ttl(int $ttl): self
149+
{
150+
$this->options['ttl'] = $ttl;
151+
152+
return $this;
153+
}
154+
155+
/**
156+
* @return $this
157+
*/
158+
public function expiration(int $expiration): self
159+
{
160+
$this->options['expiration'] = $expiration;
161+
162+
return $this;
163+
}
164+
165+
/**
166+
* @return $this
167+
*/
168+
public function data(array $data): self
169+
{
170+
$this->data = $data;
171+
172+
return $this;
173+
}
174+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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\MessageInterface;
18+
use Symfony\Component\Notifier\Message\PushMessage;
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+
final class ExpoTransport extends AbstractTransport
29+
{
30+
protected const HOST = 'exp.host/--/api/v2/push/send';
31+
32+
/** @var string|null */
33+
private $token;
34+
35+
public function __construct(string $token = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
36+
{
37+
$this->token = $token;
38+
$this->client = $client;
39+
40+
parent::__construct($client, $dispatcher);
41+
}
42+
43+
public function __toString(): string
44+
{
45+
return sprintf('expo://%s', $this->getEndpoint());
46+
}
47+
48+
public function supports(MessageInterface $message): bool
49+
{
50+
return $message instanceof PushMessage;
51+
}
52+
53+
protected function doSend(MessageInterface $message): SentMessage
54+
{
55+
if (!$message instanceof PushMessage) {
56+
throw new UnsupportedMessageTypeException(__CLASS__, PushMessage::class, $message);
57+
}
58+
59+
$endpoint = sprintf('https://%s', $this->getEndpoint());
60+
$options = ($opts = $message->getOptions()) ? $opts->toArray() : [];
61+
if (!isset($options['to'])) {
62+
$options['to'] = $message->getRecipientId();
63+
}
64+
if (null === $options['to']) {
65+
throw new InvalidArgumentException(sprintf('The "%s" transport required the "to" option to be set.', __CLASS__));
66+
}
67+
68+
$options['title'] = $message->getSubject();
69+
$options['body'] = $message->getContent();
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['error']['message'] ?? $response->getContent(false);
90+
91+
throw new TransportException('Unable to post the Expo message: '.$errorMessage, $response);
92+
}
93+
94+
$success = $response->toArray(false);
95+
96+
$sentMessage = new SentMessage($message, (string) $this);
97+
$sentMessage->setMessageId($success['data']['id']);
98+
99+
return $sentMessage;
100+
}
101+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
final class ExpoTransportFactory extends AbstractTransportFactory
23+
{
24+
/**
25+
* @return ExpoTransport
26+
*/
27+
public function create(Dsn $dsn): TransportInterface
28+
{
29+
$scheme = $dsn->getScheme();
30+
31+
if ('expo' !== $scheme) {
32+
throw new UnsupportedSchemeException($dsn, 'expo', $this->getSupportedSchemes());
33+
}
34+
35+
$token = $dsn->getUser($dsn);
36+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
37+
$port = $dsn->getPort();
38+
39+
return (new ExpoTransport($token, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
40+
}
41+
42+
protected function getSupportedSchemes(): array
43+
{
44+
return ['expo'];
45+
}
46+
}
Lines 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.

0 commit comments

Comments
 (0)
0