8000 Add OneSignal push integration · symfony/symfony@ac2c895 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac2c895

Browse files
committed
Add OneSignal push integration
1 parent adab8bd commit ac2c895

24 files changed

+697
-123
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
136136
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
137137
use Symfony\Component\Notifier\Bridge\Octopush\OctopushTransportFactory;
138+
use Symfony\Component\Notifier\Bridge\OneSignal\OneSignalTransportFactory;
138139
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
139140
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
140141
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory as SendinblueNotifierTransportFactory;
@@ -2471,6 +2472,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24712472
MobytTransportFactory::class => 'notifier.transport_factory.mobyt',
24722473
NexmoTransportFactory::class => 'notifier.transport_factory.nexmo',
24732474
OctopushTransportFactory::class => 'notifier.transport_factory.octopush',
2475+
OneSignalTransportFactory::class => 'notifier.transport_factory.onesignal',
24742476
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovhcloud',
24752477
RocketChatTransportFactory::class => 'notifier.transport_factory.rocketchat',
24762478
SendinblueNotifierTransportFactory::class => 'notifier.transport_factory.sendinblue',
@@ -2502,6 +2504,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
25022504
case 'messagebird': $package = 'message-bird'; break;
25032505
case 'messagemedia': $package = 'message-media'; break;
25042506
case 'microsoftteams': $package = 'microsoft-teams'; break;
2507+
case 'onesignal': $package = 'one-signal'; break;
25052508
case 'ovhcloud': $package = 'ovh-cloud'; break;
25062509
case 'rocketchat': $package = 'rocket-chat'; break;
25072510
case 'smsbiuras': $package = 'sms-biuras'; break;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
3737
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
3838
use Symfony\Component\Notifier\Bridge\Octopush\OctopushTransportFactory;
39+
use Symfony\Component\Notifier\Bridge\OneSignal\OneSignalTransportFactory;
3940
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
4041
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
4142
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory;
@@ -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.onesignal', OneSignalTransportFactory::class)
231+
->parent('notifier.transport_factory.abstract')
232+
->tag('texter.transport_factory')
228233
;
229234
};

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"symfony/mailer": "^5.2|^6.0",
5151
"symfony/messenger": "^5.4|^6.0",
5252
"symfony/mime": "^4.4|^5.0|^6.0",
53-
"symfony/notifier": "^5.3|^6.0",
53+
"symfony/notifier": "^5.4|^6.0",
5454
"symfony/process": "^4.4|^5.0|^6.0",
5555
"symfony/rate-limiter": "^5.2|^6.0",
5656
"symfony/security-bundle": "^5.4|^6.0",
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: 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.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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\OneSignal;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
use Symfony\Component\Notifier\Notification\Notification;
16+
17+
/**
18+
* @author Tomas Norkūnas <norkunas.tom@gmail.com>
19+
*/
20+
final class OneSignalOptions implements MessageOptionsInterface
21+
{
22+
private $options;
23+
24+
public function __construct(array $options = [])
25+
{
26+
$this->options = $options;
27+
}
28+
29+
/**
30+
* @return $this
31+
*/
32+
public static function fromNotification(Notification $notification): self
33+
{
34+
$options = new self();
35+
$options->headings(['en' => $notification->getSubject()]);
36+
$options->contents(['en' => $notification->getContent()]);
37+
38+
return $options;
39+
}
40+
41+
/**
42+
* @return $this
43+
*/
44+
public function headings(array $headings): self
45+
{
46+
$this->options['headings'] = $headings;
47+
48+
return $this;
49+
}
50+
51+
/**
52+
* @return $this
53+
*/
54+
public function contents(array $contents): self
55+
{
56+
$this->options['contents'] = $contents;
57+
58+
return $this;
59+
}
60+
61+
/**
62+
* @return $this
63+
*/
64+
public function url(string $url): self
65+
{
66+
$this->options['url'] = $url;
67+
68+
return $this;
69+
}
70+
71+
/**
72+
* @return $this
73+
*/
74+
public function data(array $data): self
75+
{
76+
$this->options['data'] = $data;
77+
78+
return $this;
79+
}
80+
81+
/**
82+
* @return $this
83+
*/
84+
public function sendAfter(\DateTimeInterface $datetime): self
85+
{
86+
$this->options['send_after'] = $datetime->format('Y-m-d H:i:sO');
87+
88+
return $this;
89+
}
90+
91+
/**
92+
* @return $this
93+
*/
94+
public function externalId(string $externalId): self
95+
{
96+
$this->options['external_id'] = $externalId;
97+
98+
return $ D7AE this;
99+
}
100+
101+
/**
102+
* @return $this
103+
*/
104+
public function recipient(string $id): self
105+
{
106+
$this->options['recipient_id'] = $id;
107+
108+
return $this;
109+
}
110+
111+
public function getRecipientId(): ?string
112+
{
113+
return $this->options['recipient_id'] ?? null;
114+
}
115+
116+
public function toArray(): array
117+
{
118+
$options = $this->options;
119+
unset($options['recipient_id']);
120+
121+
return $options;
122+
}
123+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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\OneSignal;
13+
14+
use Symfony\Component\Notifier\Exception\LogicException;
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 Tomas Norkūnas <norkunas.tom@gmail.com>
27+
*/
28+
final class OneSignalTransport extends AbstractTransport
29+
{
30+
protected const HOST = 'onesignal.com';
31+
32+
private $appId;
33+
private $apiKey;
34+
private $defaultRecipientId;
35+
36+
public function __construct(string $appId, string $apiKey, string $defaultRecipientId = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
37+
{
38+
$this->appId = $appId;
39+
$this->apiKey = $apiKey;
40+
$this->defaultRecipientId = $defaultRecipientId;
41+
42+
parent::__construct($client, $dispatcher);
43+
}
44+
45+
public function __toString(): string
46+
{
47+
if (null === $this->defaultRecipientId) {
48+
return sprintf('onesignal://%s@%s', urlencode($this->appId), $this->getEndpoint());
49+
}
50+
51+
return sprintf('onesignal://%s@%s?recipientId=%s', urlencode($this->appId), $this->getEndpoint(), $this->defaultRecipientId);
52+
}
53+
54+
public function supports(MessageInterface $message): bool
55+
{
56+
return $message instanceof PushMessage && (null !== $this->defaultRecipientId || ($message->getOptions() instanceof OneSignalOptions && null !== $message->getOptions()->getRecipientId()));
57+
}
58+
59+
/**
60+
* @see https://documentation.onesignal.com/reference/create-notification
61+
*/
62+
protected function doSend(MessageInterface $message): SentMessage
63+
{
64+
if (!$message instanceof PushMessage) {
65+
throw new UnsupportedMessageTypeException(__CLASS__, PushMessage::class, $message);
66+
}
67+
68+
if ($message->getOptions() && !$message->getOptions() instanceof OneSignalOptions) {
69+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, OneSignalOptions::class));
70+
}
71+
72+
if (!($opts = $message->getOptions()) && $notification = $message->getNotification()) {
73+
$opts = OneSignalOptions::fromNotification($notification);
74+
}
75+
76+
$recipientId = $message->getRecipientId() ?? $this->defaultRecipientId;
77+
78+
if (null === $recipientId) {
79+
throw new LogicException(sprintf('The "%s" transport should have configured `defaultRecipientId` via DSN or provided with message options.', __CLASS__));
80+
}
81+
82+
$options = $opts ? $opts->toArray() : [];
83+
$options['app_id'] = $this->appId;
84+
$options['include_player_ids'] = [$recipientId];
85+
86+
if (!isset($options['headings'])) {
87+
$options['headings'] = ['en' => $message->getSubject()];
88+
}
89+
if (!isset($options['contents'])) {
90+
$options['contents'] = ['en' => $message->getContent()];
91+
}
92+
93+
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/api/v1/notifications', [
94+
'headers' => [
95+
'Accept' => 'application/json',
96+
'Authorization' => 'Basic '.$this->apiKey,
97+
],
98+
'json' => $options,
99+
]);
100+
101+
try {
102+
$statusCode = $response->getStatusCode();
103+
} catch (TransportExceptionInterface $e) {
104+
throw new TransportException('Could not reach the remote OneSignal server.', $response, 0, $e);
105+
}
106+
107+
if (200 !== $statusCode) {
108+
throw new TransportException(sprintf('Unable to send the OneSignal push notification: "%s".', $response->getContent(false)), $response);
109+
}
110+
111+
$result = $response->toArray(false);
112+
113+
if (empty($result['id'])) {
114+
throw new TransportException(sprintf('Unable to send the OneSignal push notification: "%s".', $response->getContent(false)), $response);
115+
}
116+
117+
$sentMessage = new SentMessage($message, (string) $this);
118+
$sentMessage->setMessageId($result['id']);
119+
120+
return $sentMessage;
121+
}
122+
}
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\OneSignal;
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 Tomas Norkūnas <norkunas.tom@gmail.com>
21+
*/
22+
final class OneSignalTransportFactory extends AbstractTransportFactory
23+
{
24+
/**
25+
* @return OneSignalTransport
26+
*/
27+
public function create(Dsn $dsn): TransportInterface
28+
{
29+
if ('onesignal' !== $dsn->getScheme()) {
30+
throw new UnsupportedSchemeException($dsn, 'onesignal', $this->getSupportedSchemes());
31+
}
32+
33+
$appId = $this->getUser($dsn);
34+
$apiKey = $this->getPassword($dsn);
35+
$defaultRecipientId = $dsn->getOption('defaultRecipientId');
36+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
37+
$port = $dsn->getPort();
38+
39+
return (new OneSignalTransport($appId, $apiKey, $defaultRecipientId, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
40+
}
41+
42+
protected function getSupportedSchemes(): array
43+
{
44+
return ['onesignal'];
45+
}
46+
}

0 commit comments

Comments
 (0)
0