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 847c011

Browse filesBrowse files
committed
feature #39402 [Notifier] Add push channel to notifier (norkunas)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Notifier] Add push channel to notifier | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #... | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> I am proposing to add a new push channel for sending web/mobile push notifications to users. This will open possibility to integrate providers like OneSignal, WonderPush, etc. And as requested added the first implementation. Commits ------- e8638ab [Notifier] Add push channel to notifier
2 parents b268a69 + e8638ab commit 847c011
Copy full SHA for 847c011

24 files changed

+931
-1
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
137137
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
138138
use Symfony\Component\Notifier\Bridge\Octopush\OctopushTransportFactory;
139+
use Symfony\Component\Notifier\Bridge\OneSignal\OneSignalTransportFactory;
139140
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
140141
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
141142
use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory as SendinblueNotifierTransportFactory;
@@ -2434,6 +2435,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24342435
$container->getDefinition('notifier.channel.email')->setArgument(0, null);
24352436
}
24362437
$container->getDefinition('notifier.channel.sms')->setArgument(0, null);
2438+
$container->getDefinition('notifier.channel.push')->setArgument(0, null);
24372439
}
24382440

24392441
$container->getDefinition('notifier.channel_policy')->setArgument(0, $config['channel_policy']);
@@ -2470,6 +2472,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24702472
MobytTransportFactory::class => 'notifier.transport_factory.mobyt',
24712473
NexmoTransportFactory::class => 'notifier.transport_factory.nexmo',
24722474
OctopushTransportFactory::class => 'notifier.transport_factory.octopush',
2475+
OneSignalTransportFactory::class => 'notifier.transport_factory.onesignal',
24732476
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovhcloud',
24742477
RocketChatTransportFactory::class => 'notifier.transport_factory.rocketchat',
24752478
SendinblueNotifierTransportFactory::class => 'notifier.transport_factory.sendinblue',
@@ -2502,6 +2505,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
25022505
case 'messagebird': $package = 'message-bird'; break;
25032506
case 'messagemedia': $package = 'message-media'; break;
25042507
case 'microsoftteams': $package = 'microsoft-teams'; break;
2508+
case 'onesignal': $package = 'one-signal'; break;
25052509
case 'ovhcloud': $package = 'ovh-cloud'; break;
25062510
case 'rocketchat': $package = 'rocket-chat'; break;
25072511
case 'smsbiuras': $package = 'sms-biuras'; break;

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier.php
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
use Symfony\Component\Notifier\Channel\ChannelPolicy;
1717
use Symfony\Component\Notifier\Channel\ChatChannel;
1818
use Symfony\Component\Notifier\Channel\EmailChannel;
19+
use Symfony\Component\Notifier\Channel\PushChannel;
1920
use Symfony\Component\Notifier\Channel\SmsChannel;
2021
use Symfony\Component\Notifier\Chatter;
2122
use Symfony\Component\Notifier\ChatterInterface;
2223
use Symfony\Component\Notifier\EventListener\NotificationLoggerListener;
2324
use Symfony\Component\Notifier\EventListener\SendFailedMessageToNotifierListener;
2425
use Symfony\Component\Notifier\Message\ChatMessage;
26+
use Symfony\Component\Notifier\Message\PushMessage;
2527
use Symfony\Component\Notifier\Message\SmsMessage;
2628
use Symfony\Component\Notifier\Messenger\MessageHandler;
2729
use Symfony\Component\Notifier\Notifier;
@@ -57,6 +59,10 @@
5759
->args([service('mailer.transports'), service('messenger.default_bus')->ignoreOnInvalid()])
5860
->tag('notifier.channel', ['channel' => 'email'])
5961

62+
->set('notifier.channel.push', PushChannel::class)
63+
->args([service('texter.transports'), service('messenger.default_bus')->ignoreOnInvalid()])
64+
->tag('notifier.channel', ['channel' => 'push'])
65+
6066
->set('notifier.monolog_handler', NotifierHandler::class)
6167
->args([service('notifier')])
6268

@@ -103,6 +109,10 @@
103109
->args([service('texter.transports')])
104110
->tag('messenger.message_handler', ['handles' => SmsMessage::class])
105111

112+
->set('texter.messenger.push_handler', MessageHandler::class)
113+
->args([service('texter.transports')])
114+
->tag('messenger.message_handler', ['handles' => PushMessage::class])
115+
106116
->set('notifier.logger_notification_listener', NotificationLoggerListener::class)
107117
->tag('kernel.event_subscriber')
108118
;

‎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;
@@ -230,5 +231,9 @@
230231
->set('notifier.transport_factory.sms77', Sms77TransportFactory::class)
231232
->parent('notifier.transport_factory.abstract')
232233
->tag('texter.transport_factory')
234+
235+
->set('notifier.transport_factory.onesignal', OneSignalTransportFactory::class)
236+
->parent('notifier.transport_factory.abstract')
237+
->tag('texter.transport_factory')
233238
;
234239
};

‎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.4|^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.
+123Lines 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 $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+
}
+122Lines 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+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.