-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Notifier] [Firebase] Add 'HTTP v1' api endpoint #53336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3e5bf69
ac9002f
73525c9
ec11593
b035895
17f0ea8
6957bd4
a068ca4
495e796
0ff2696
47075c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,16 +24,20 @@ | |
|
||
/** | ||
* @author Jeroen Spee <https://github.com/Jeroeny> | ||
* @author Cesur APAYDIN <https://github.com/cesurapp> | ||
*/ | ||
final class FirebaseTransport extends AbstractTransport | ||
{ | ||
protected const HOST = 'fcm.googleapis.com/fcm/send'; | ||
protected const HOST = 'fcm.googleapis.com/v1/projects/project_id/messages:send'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at all the other Notifier transports, there seems to be almost none that use a full URL in the |
||
|
||
private array $credentials; | ||
|
||
public function __construct(#[\SensitiveParameter] array $credentials, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) | ||
{ | ||
$this->credentials = $credentials; | ||
$this->client = $client; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
$this->setHost(str_replace('project_id', $credentials['project_id'], $this->getDefaultHost())); | ||
|
||
public function __construct( | ||
#[\SensitiveParameter] private string $token, | ||
?HttpClientInterface $client = null, | ||
?EventDispatcherInterface $dispatcher = null, | ||
) { | ||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
|
@@ -53,21 +57,20 @@ protected function doSend(MessageInterface $message): SentMessage | |
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); | ||
} | ||
|
||
$endpoint = \sprintf('https://%s', $this->getEndpoint()); | ||
$options = $message->getOptions()?->toArray() ?? []; | ||
$options['to'] = $message->getRecipientId(); | ||
$endpoint = sprintf('https://%s', $this->getEndpoint()); | ||
|
||
if (!$options['to']) { | ||
throw new InvalidArgumentException(\sprintf('The "%s" transport required the "to" option to be set.', __CLASS__)); | ||
// Generate Options | ||
$options = $message->getOptions()?->toArray() ?? []; | ||
if (!$options['token'] && !$options['topic']) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it not It generates this warning:
|
||
throw new InvalidArgumentException(sprintf('The "%s" transport required the "token" or "topic" option to be set.', __CLASS__)); | ||
} | ||
$options['notification']['body'] = $message->getSubject(); | ||
$options['data'] ??= []; | ||
|
||
// Send | ||
$response = $this->client->request('POST', $endpoint, [ | ||
'headers' => [ | ||
'Authorization' => \sprintf('key=%s', $this->token), | ||
], | ||
'json' => array_filter($options), | ||
'headers' => ['Authorization' => sprintf('Bearer %s', $this->getJwtToken())], | ||
'json' => array_filter(['message' => $options]), | ||
]); | ||
|
||
try { | ||
|
@@ -87,14 +90,51 @@ protected function doSend(MessageInterface $message): SentMessage | |
} | ||
|
||
if (null !== $errorMessage) { | ||
throw new TransportException('Unable to post the Firebase message: '.$errorMessage, $response); | ||
throw new TransportException('Unable to post the Firebase message: ' . $errorMessage, $response); | ||
} | ||
|
||
$success = $response->toArray(false); | ||
|
||
$sentMessage = new SentMessage($message, (string) $this); | ||
$sentMessage = new SentMessage($message, (string)$this); | ||
$sentMessage->setMessageId($success['results'][0]['message_id'] ?? ''); | ||
|
||
return $sentMessage; | ||
} | ||
|
||
private function getJwtToken(): string | ||
{ | ||
$time = time(); | ||
$payload = [ | ||
'iss' => $this->credentials['client_email'], | ||
'sub' => $this->credentials['client_email'], | ||
'aud' => 'https://fcm.googleapis.com/', | ||
'iat' => $time, | ||
'exp' => $time + 3600, | ||
'kid' => $this->credentials['private_key_id'], | ||
]; | ||
|
||
$header = $this->urlSafeEncode(['alg' => 'RS256', 'typ' => 'JWT']); | ||
$payload = $this->urlSafeEncode($payload); | ||
openssl_sign($header . '.' . $payload, $signature, openssl_pkey_get_private($this->encodePk($this->credentials['private_key'])), OPENSSL_ALGO_SHA256); | ||
$signature = $this->urlSafeEncode($signature); | ||
|
||
return $header . '.' . $payload . '.' . $signature; | ||
} | ||
|
||
protected function urlSafeEncode(string|array $data): string | ||
{ | ||
if (is_array($data)) { | ||
$data = json_encode($data, JSON_UNESCAPED_SLASHES); | ||
} | ||
|
||
return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); | ||
} | ||
|
||
protected function encodePk(string $privateKey): string | ||
{ | ||
$text = explode('-----', $privateKey); | ||
$text[2] = str_replace(['\n', '_', ' '], ["\n", "\n", '+'], $text[2]); | ||
|
||
return implode('-----', $text); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,25 @@ Firebase Notifier | |
|
||
Provides [Firebase](https://firebase.google.com) integration for Symfony Notifier. | ||
|
||
DSN example | ||
JWT DSN Example (HTTP v1) | ||
----------- | ||
|
||
``` | ||
FIREBASE_DSN=firebase://USERNAME:PASSWORD@default | ||
FIREBASE_DSN=firebase://<CLIENT_EMAIL>?project_id=<PROJECT_ID>&private_key_id=<PRIVATE_KEY_ID>&private_key=<PRIVATE_KEY> | ||
FIREBASE_DSN=firebase://[email protected]?project_id=<PROJECT_ID>&private_key_id=<PRIVATE_KEY_ID>&private_key=<PRIVATE_KEY> | ||
``` | ||
|
||
where: | ||
- `USERNAME` is your Firebase username | ||
- `PASSWORD` is your Firebase password | ||
Since __"private_key"__ is long, you must write it in a single line with "\n". Example: | ||
``` | ||
-----BEGIN RSA PRIVATE KEY-----\n.....\n....\n-----END RSA PRIVATE KEY----- | ||
``` | ||
|
||
__Required Options:__ | ||
* client_email | ||
* project_id | ||
* private_key_id | ||
* private_key | ||
|
||
|
||
Adding Interactions to a Message | ||
-------------------------------- | ||
|
@@ -27,7 +36,7 @@ use Symfony\Component\Notifier\Bridge\Firebase\Notification\AndroidNotification; | |
$chatMessage = new ChatMessage(''); | ||
|
||
// Create AndroidNotification options | ||
$androidOptions = (new AndroidNotification('/topics/news', [])) | ||
$androidOptions = (new AndroidNotification('/topics/news', [], [], true)) | ||
->icon('myicon') | ||
->sound('default') | ||
->tag('myNotificationId') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,19 +27,19 @@ public function createFactory(): FirebaseTransportFactory | |
public static function createProvider(): iterable | ||
{ | ||
yield [ | ||
'firebase://host.test', | ||
'firebase://username:[email protected]', | ||
'firebase://fcm.googleapis.com/v1/projects/<PROJECT_ID>/messages:send', | ||
'firebase://[email protected]?project_id=<PROJECT_ID>&private_key_id=<PRIVATE_KEY_ID>&private_key=<PRIVATE_KEY>', | ||
]; | ||
} | ||
|
||
public static function supportsProvider(): iterable | ||
{ | ||
yield [true, 'firebase://username:password@default']; | ||
yield [false, 'somethingElse://username:password@default']; | ||
yield [true, 'firebase://client_email?project_id=1']; | ||
yield [false, 'somethingElse://client_email?project_id=1']; | ||
} | ||
|
||
public static function unsupportedSchemeProvider(): iterable | ||
{ | ||
yield ['somethingElse://username:password@default']; | ||
yield ['somethingElse://client_email']; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,12 +30,17 @@ final class FirebaseTransportTest extends TransportTestCase | |
{ | ||
public static function createTransport(?HttpClientInterface $client = null): FirebaseTransport | ||
{ | ||
return new FirebaseTransport('username:password', $client ?? new MockHttpClient()); | ||
return new FirebaseTransport([ | ||
'client_email' => '[email protected]', | ||
'project_id' => 'test_project', | ||
'private_key_id' => 'sdas7d6a8ds6ds78a', | ||
'private_key' => "-----BEGIN RSA PRIVATE KEY-----\nMIICWwIBAAKBgGN4fgq4BFQwjK7kzWUYSFE1ryGIBtUScY5TqLY2BAROBnZS+SIa\nH4VcZJStPUwjtsVxJTf57slhMM5FbAOQkWFMmRlHGWc7EZy6UMMvP8FD21X3Ty9e\nZzJ/Be30la1Uy7rechBh3RN+Y3rSKV+gDmsjdo5/4Jekj4LfluDXbwVJAgMBAAEC\ngYA5SqY2IEUGBKyS81/F8ZV9iNElHAbrZGMZWeAbisMHg7U/I40w8iDjnBKme52J\npCxaTk/kjMTXIm6M7/lFmFfTHgl5WLCimu2glMyKFM2GBYX/cKx9RnI36q3uJYml\n1G1f2H7ALurisenEqMaq8bdyApd/XNqcijogfsZ1K/irTQJBAKEQFkqNDgwUgAwr\njhG/zppl5yEJtP+Pncp/2t/s6khk0q8N92xw6xl8OV/ww+rwlJB3IKVKw903LztQ\nP1D3zpMCQQCeGlOvMx9XxiktNIkdXekGP/bFUR9/u0ABaYl9valZ2B3yZzujJJHV\n0EtyKGorT39wWhWY7BI8NTYgivCIWGozAkEAhMnOlwhUXIFKUL5YEyogHAuH0yU9\npLWzUhC3U4bwYV8+lDTfmPg/3HMemorV/Az9b13H/H73nJqyxiQTD54/IQJAZUX/\n7O4WWac5oRdR7VnGdpZqgCJixvMvILh1tfHTlRV2uVufO/Wk5Q00BsAUogGeZF2Q\nEBDH7YE4VsgpI21fOQJAJdSB7mHvStlYCQMEAYWCWjk+NRW8fzZCkQkqzOV6b9dw\nDFp6wp8aLw87hAHUz5zXTCRYi/BpvDhfP6DDT2sOaw==\n-----END RSA PRIVATE KEY-----" | ||
], $client ?? new MockHttpClient()); | ||
} | ||
|
||
public static function toStringProvider(): iterable | ||
{ | ||
yield ['firebase://fcm.googleapis.com/fcm/send', self::createTransport()]; | ||
yield ['firebase://fcm.googleapis.com/v1/projects/test_project/messages:send', self::createTransport()]; | ||
} | ||
|
||
public static function supportedMessagesProvider(): iterable | ||
|
@@ -56,7 +61,7 @@ public function testSendWithErrorThrowsTransportException(ResponseInterface $res | |
{ | ||
$this->expectException(TransportException::class); | ||
|
||
$client = new MockHttpClient(static fn (): ResponseInterface => $response); | ||
$client = new MockHttpClient(static fn(): ResponseInterface => $response); | ||
$options = new class('recipient-id', []) extends FirebaseOptions {}; | ||
|
||
$transport = self::createTransport($client); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it make sense to parse
$tokenOrTopic
for a string like/topic/...
and add a deprecation message (but handle the flag correctly)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If set to "$useTopic = true" no deprecation message will be needed. The default topic is used in the previous version.