Skip to content
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

Remove reliance on the CloudMessage class when handling messages #934

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Please read about the future of the Firebase Admin PHP SDK on the

## [Unreleased]

### Changed

* The Messaging component doesn't rely on the `CloudMessage` class for message handling anymore. If you provide a
message as an array and it has an error, the Firebase API will report it. You can still use the `CloudMessage`
class as a message builder

## [7.15.0] - 2024-09-11

### Added
Expand Down
63 changes: 49 additions & 14 deletions src/Firebase/Messaging.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
use Kreait\Firebase\Messaging\ApiClient;
use Kreait\Firebase\Messaging\AppInstance;
use Kreait\Firebase\Messaging\AppInstanceApiClient;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Message;
use Kreait\Firebase\Messaging\MessageTarget;
use Kreait\Firebase\Messaging\MulticastSendReport;
use Kreait\Firebase\Messaging\Processor\SetApnsContentAvailableIfNeeded;
use Kreait\Firebase\Messaging\Processor\SetApnsPushTypeIfNeeded;
use Kreait\Firebase\Messaging\RawMessageFromArray;
use Kreait\Firebase\Messaging\RegistrationToken;
use Kreait\Firebase\Messaging\RegistrationTokens;
use Kreait\Firebase\Messaging\SendReport;
Expand Down Expand Up @@ -68,15 +68,13 @@

public function sendMulticast($message, $registrationTokens, bool $validateOnly = false): MulticastSendReport
{
$message = CloudMessage::fromArray(
Json::decode(Json::encode($this->makeMessage($message)), true),
);
$message = $message instanceof Message ? $message : new RawMessageFromArray($message);

Check warning on line 71 in src/Firebase/Messaging.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging.php#L71

Added line #L71 was not covered by tests
$registrationTokens = RegistrationTokens::fromValue($registrationTokens);

$messages = [];

foreach ($registrationTokens as $registrationToken) {
$messages[] = $message->withChangedTarget(MessageTarget::TOKEN, $registrationToken->value());
$messages[] = $this->withChangedTarget($message, MessageTarget::TOKEN, $registrationToken->value());

Check warning on line 77 in src/Firebase/Messaging.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging.php#L77

Added line #L77 was not covered by tests
}

return $this->sendAll($messages, $validateOnly);
Expand All @@ -94,14 +92,14 @@

$json = Json::decode((string) $response->getBody(), true);

$sendReports[$index] = SendReport::success($message->target(), $json, $message);
$sendReports[$index] = SendReport::success($this->getTarget($message), $json, $message);

Check warning on line 95 in src/Firebase/Messaging.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging.php#L95

Added line #L95 was not covered by tests
},
'rejected' => function (Throwable $reason, int $index) use ($messages, &$sendReports): void {
$message = $messages[$index];

$error = $this->exceptionConverter->convertException($reason);

$sendReports[$index] = SendReport::failure($message->target(), $error, $message);
$sendReports[$index] = SendReport::failure($this->getTarget($message), $error, $message);

Check warning on line 102 in src/Firebase/Messaging.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging.php#L102

Added line #L102 was not covered by tests
},
];

Expand All @@ -124,7 +122,7 @@
{
$tokens = RegistrationTokens::fromValue($registrationTokenOrTokens);

$report = $this->sendMulticast(CloudMessage::new(), $tokens, true);
$report = $this->sendMulticast(new RawMessageFromArray([]), $tokens, true);

Check warning on line 125 in src/Firebase/Messaging.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging.php#L125

Added line #L125 was not covered by tests

return [
'valid' => $report->validTokens(),
Expand Down Expand Up @@ -220,7 +218,7 @@
/**
* @param iterable<Message|array<non-empty-string, mixed>> $messages
*
* @return list<CloudMessage>
* @return list<Message>
*/
private function ensureMessages(iterable $messages): array
{
Expand All @@ -238,18 +236,17 @@
*
* @throws InvalidArgumentException
*/
private function makeMessage(Message|array $message): CloudMessage
private function makeMessage(Message|array $message): Message
{
$message = $message instanceof Message ? $message : CloudMessage::fromArray($message);
$message = $message instanceof Message ? $message : new RawMessageFromArray($message);

$message = (new SetApnsPushTypeIfNeeded())($message);
$message = (new SetApnsContentAvailableIfNeeded())($message);

return CloudMessage::fromArray(Json::decode(JSON::encode($message->jsonSerialize()), true));
return (new SetApnsContentAvailableIfNeeded())($message);
}

/**
* @param iterable<CloudMessage> $messages
* @param iterable<Message> $messages
* @return callable(): list<RequestInterface>
*/
private function createSendRequests(iterable $messages, bool $validateOnly): callable
Expand All @@ -269,4 +266,42 @@
|| array_key_exists(MessageTarget::TOKEN, $check)
|| array_key_exists(MessageTarget::TOPIC, $check);
}

private function withChangedTarget(Message $message, string $target, string $value): Message

Check warning on line 270 in src/Firebase/Messaging.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging.php#L270

Added line #L270 was not covered by tests
{
$message = Json::decode(Json::encode($message), true);

Check warning on line 272 in src/Firebase/Messaging.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging.php#L272

Added line #L272 was not covered by tests

unset(
$message[MessageTarget::CONDITION],
$message[MessageTarget::TOKEN],
$message[MessageTarget::TOPIC],

Check warning on line 277 in src/Firebase/Messaging.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging.php#L274-L277

Added lines #L274 - L277 were not covered by tests
);

$message[$target] = $value;

Check warning on line 280 in src/Firebase/Messaging.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging.php#L280

Added line #L280 was not covered by tests

return new RawMessageFromArray($message);

Check warning on line 282 in src/Firebase/Messaging.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging.php#L282

Added line #L282 was not covered by tests
}

private function getTarget(Message $message): MessageTarget

Check warning on line 285 in src/Firebase/Messaging.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging.php#L285

Added line #L285 was not covered by tests
{
$message = Json::decode(Json::encode($message), true);

Check warning on line 287 in src/Firebase/Messaging.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging.php#L287

Added line #L287 was not covered by tests

$condition = $message[MessageTarget::CONDITION] ?? null;
$token = $message[MessageTarget::TOKEN] ?? null;
$topic = $message[MessageTarget::TOPIC] ?? null;

Check warning on line 291 in src/Firebase/Messaging.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging.php#L289-L291

Added lines #L289 - L291 were not covered by tests

if (is_string($condition) && $condition !== '') {
return MessageTarget::with(MessageTarget::CONDITION, $condition);

Check warning on line 294 in src/Firebase/Messaging.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging.php#L293-L294

Added lines #L293 - L294 were not covered by tests
}

if (is_string($token) && $token !== '') {
return MessageTarget::with(MessageTarget::TOKEN, $token);

Check warning on line 298 in src/Firebase/Messaging.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging.php#L297-L298

Added lines #L297 - L298 were not covered by tests
}

if (is_string($topic) && $topic !== '') {
return MessageTarget::with(MessageTarget::TOPIC, $topic);

Check warning on line 302 in src/Firebase/Messaging.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging.php#L301-L302

Added lines #L301 - L302 were not covered by tests
}

return MessageTarget::with(MessageTarget::UNKNOWN, 'unknown');

Check warning on line 305 in src/Firebase/Messaging.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging.php#L305

Added line #L305 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use Beste\Json;
use Kreait\Firebase\Messaging\ApnsConfig;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Message;
use Kreait\Firebase\Messaging\MessageData;
use Kreait\Firebase\Messaging\Notification;
use Kreait\Firebase\Messaging\RawMessageFromArray;

use function is_array;

Expand Down Expand Up @@ -45,7 +45,9 @@ public function __invoke(Message $message): Message

$apnsConfig = $apnsConfig->withApsField('content-available', 1);

return CloudMessage::fromArray($payload)->withApnsConfig($apnsConfig);
$payload['apns'] = $apnsConfig->toArray();

return new RawMessageFromArray($payload);
}

/**
Expand Down
12 changes: 10 additions & 2 deletions src/Firebase/Messaging/Processor/SetApnsPushTypeIfNeeded.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use Beste\Json;
use Kreait\Firebase\Messaging\ApnsConfig;
use Kreait\Firebase\Messaging\CloudMessage;
use Kreait\Firebase\Messaging\Message;
use Kreait\Firebase\Messaging\MessageData;
use Kreait\Firebase\Messaging\Notification;
use Kreait\Firebase\Messaging\RawMessageFromArray;

use function is_array;

Expand All @@ -25,6 +25,10 @@
{
$payload = Json::decode(Json::encode($message), true);

if (!is_array($payload)) {
return $message;

Check warning on line 29 in src/Firebase/Messaging/Processor/SetApnsPushTypeIfNeeded.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging/Processor/SetApnsPushTypeIfNeeded.php#L29

Added line #L29 was not covered by tests
}

$notification = $this->getNotification($payload);
$messageData = $this->getMessageData($payload);
$apnsConfig = $this->getApnsConfig($payload);
Expand All @@ -46,9 +50,13 @@
$apnsConfig = $apnsConfig->withHeader('apns-push-type', 'alert');
} elseif ($isBackgroundMessage) {
$apnsConfig = $apnsConfig->withHeader('apns-push-type', 'background');
} else {
return $message;

Check warning on line 54 in src/Firebase/Messaging/Processor/SetApnsPushTypeIfNeeded.php

View check run for this annotation

Codecov / codecov/patch

src/Firebase/Messaging/Processor/SetApnsPushTypeIfNeeded.php#L54

Added line #L54 was not covered by tests
}

return CloudMessage::fromArray($payload)->withApnsConfig($apnsConfig);
$payload['apns'] = $apnsConfig->toArray();

return new RawMessageFromArray($payload);
}

/**
Expand Down
Loading