-
-
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); | ||
} | ||
} |
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.