Skip to content

Navigation Menu

Sign in
Appearance settings

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 1939c02

Browse filesBrowse files
committed
Add OneSignal push integration
1 parent 4b4c3e6 commit 1939c02
Copy full SHA for 1939c02

24 files changed

+666
-123
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
135135
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
136136
use Symfony\Component\Notifier\Bridge\Octopush\OctopushTransportFactory;
137+
use Symfony\Component\Notifier\Bridge\OneSignal\OneSignalTransportFactory;
137138
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
138139
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
139140
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory as SendinblueNotifierTransportFactory;
@@ -2453,6 +2454,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24532454
MobytTransportFactory::class => 'notifier.transport_factory.mobyt',
24542455
NexmoTransportFactory::class => 'notifier.transport_factory.nexmo',
24552456
OctopushTransportFactory::class => 'notifier.transport_factory.octopush',
2457+
OneSignalTransportFactory::class => 'notifier.transport_factory.onesignal',
24562458
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovhcloud',
24572459
RocketChatTransportFactory::class => 'notifier.transport_factory.rocketchat',
24582460
SendinblueNotifierTransportFactory::class => 'notifier.transport_factory.sendinblue',
@@ -2484,6 +2486,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24842486
case 'messagebird': $package = 'message-bird'; break;
24852487
case 'messagemedia': $package = 'message-media'; break;
24862488
case 'microsoftteams': $package = 'microsoft-teams'; break;
2489+
case 'onesignal': $package = 'one-signal'; break;
24872490
case 'ovhcloud': $package = 'ovh-cloud'; break;
24882491
case 'rocketchat': $package = 'rocket-chat'; break;
24892492
case 'smsbiuras': $package = 'sms-biuras'; break;

‎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
@@ -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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/composer.json
+1-1Lines 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.3|^6.0",
+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.4
5+
---
6+
7+
* Add the bridge
+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.
+99Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
public static function fromNotification(Notification $notification): self
30+
{
31+
$options = new self();
32+
$options->headings(['en' => $notification->getSubject()]);
33+
$options->contents(['en' => $notification->getContent()]);
34+
35+
return $options;
36+
}
37+
38+
public function headings(array $headings): self
39+
{
40+
$this->options['headings'] = $headings;
41+
42+
return $this;
43+
}
44+
45+
public function contents(array $contents): self
46+
{
47+
$this->options['contents'] = $contents;
48+
49+
return $this;
50+
}
51+
52+
public function url(string $url): self
53+
{
54+
$this->options['url'] = $url;
55+
56+
return $this;
57+
}
58+
59+
public function data(array $data): self
60+
{
61+
$this->options['data'] = $data;
62+
63+
return $this;
64+
}
65+
66+
public function sendAfter(\DateTimeInterface $datetime): self
67+
{
68+
$this->options['send_after'] = $datetime->format('Y-m-d H:i:sO');
69+
70+
return $this;
71+
}
72+
73+
public function externalId(string $externalId): self
74+
{
75+
$this->options['external_id'] = $externalId;
76+
77+
return $this;
78+
}
79+
80+
public function recipient(string $id): self
81+
{
82+
$this->options['recipient_id'] = $id;
83+
84+
return $this;
85+
}
86+
87+
public function getRecipientId(): ?string
88+
{
89+
return $this->options['recipient_id'] ?? null;
90+
}
91+
92+
public function toArray(): array
93+
{
94+
$options = $this->options;
95+
unset($options['recipient_id']);
96+
97+
return $options;
98+
}
99+
}
+115Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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\HttpClientInterface;
23+
24+
/**
25+
* @author Tomas Norkūnas <norkunas.tom@gmail.com>
26+
*/
27+
final class OneSignalTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'onesignal.com';
30+
31+
private $appId;
32+
private $apiKey;
33+
private $defaultRecipientId;
34+
35+
public function __construct(string $appId, string $apiKey, string $defaultRecipientId = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
36+
{
37+
$this->appId = $appId;
38+
$this->apiKey = $apiKey;
39+
$this->defaultRecipientId = $defaultRecipientId;
40+
41+
parent::__construct($client, $dispatcher);
42+
}
43+
44+
public function __toString(): string
45+
{
46+
if (null === $this->defaultRecipientId) {
47+
return sprintf('onesignal://%s@%s', urlencode($this->appId), $this->getEndpoint());
48+
}
49+
50+
return sprintf('onesignal://%s@%s?recipientId=%s', urlencode($this->appId), $this->getEndpoint(), $this->defaultRecipientId);
51+
}
52+
53+
public function supports(MessageInterface $message): bool
54+
{
55+
return $message instanceof PushMessage && (null !== $this->defaultRecipientId || ($message->getOptions() instanceof OneSignalOptions && null !== $message->getOptions()->getRecipientId()));
56+
}
57+
58+
/**
59+
* @see https://documentation.onesignal.com/reference/create-notification
60+
*/
61+
protected function doSend(MessageInterface $message): SentMessage
62+
{
63+
if (!$message instanceof PushMessage) {
64+
throw new UnsupportedMessageTypeException(__CLASS__, PushMessage::class, $message);
65+
}
66+
67+
if ($message->getOptions() && !$message->getOptions() instanceof OneSignalOptions) {
68+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, OneSignalOptions::class));
69+
}
70+
71+
if (!($opts = $message->getOptions()) && $notification = $message->getNotification()) {
72+
$opts = OneSignalOptions::fromNotification($notification);
73+
}
74+
75+
$recipientId = $message->getRecipientId() ?? $this->defaultRecipientId;
76+
77+
if (null === $recipientId) {
78+
throw new LogicException(sprintf('The "%s" transport should have configured `defaultRecipientId` via DSN or provided with message options.', __CLASS__));
79+
}
80+
81+
$options = $opts ? $opts->toArray() : [];
82+
$options['app_id'] = $this->appId;
83+
$options['include_player_ids'] = [$recipientId];
84+
85+
if (!isset($options['headings'])) {
86+
$options['headings'] = ['en' => $message->getSubject()];
87+
}
88+
if (!isset($options['contents'])) {
89+
$options['contents'] = ['en' => $message->getContent()];
90+
}
91+
92+
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/api/v1/notifications', [
93+
'headers' => [
94+
'Accept' => 'application/json',
95+
'Authorization' => 'Basic '.$this->apiKey,
96+
],
97+
'json' => $options,
98+
]);
99+
100+
if (200 !== $response->getStatusCode()) {
101+
throw new TransportException(sprintf('Unable to send the OneSignal push notification: "%s".', $response->getContent(false)), $response);
102+
}
103+
104+
$result = $response->toArray(false);
105+
106+
if (empty($result['id'])) {
107+
throw new TransportException(sprintf('Unable to send the OneSignal push notification: "%s".', $response->getContent(false)), $response);
108+
}
109+
110+
$sentMessage = new SentMessage($message, (string) $this);
111+
$sentMessage->setMessageId($result['id']);
112+
113+
return $sentMessage;
114+
}
115+
}
+46Lines 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+
}
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
OneSignal Notifier
2+
==================
3+
4+
Provides [OneSignal](https://documentation.onesignal.com/reference/create-notification) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
ONESIGNAL_DSN=onesignal://APP_ID:API_KEY@default?defaultRecipientId=DEFAULT_RECIPIENT_ID
11+
```
12+
13+
where:
14+
- `APP_ID` is your OneSignal application id
15+
- `API_KEY` is your OneSignal application auth key
16+
- `DEFAULT_RECIPIENT_ID` is an optional default recipient
17+
18+
19+
Resources
20+
---------
21+
22+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
23+
* [Report issues](https://github.com/symfony/symfony/issues) and
24+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
25+
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.