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

adds expiration message for fcm provider #74

Merged
merged 5 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/Utopia/Messaging/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract public function getMaxMessagesPerRequest(): int;
* deliveredTo: int,
* type: string,
* results: array<array<string, mixed>>
* }>
* }> GEOSMS adapter returns an array of results keyed by adapter name.
*
* @throws \Exception
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Utopia/Messaging/Adapter/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public function getMessageType(): string
return PushMessage::class;
}

protected static function getExpiredErrorMessage(): string
{
return 'Expired device token.';
}
stnguyen90 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Send a push message.
*
Expand Down
18 changes: 3 additions & 15 deletions src/Utopia/Messaging/Adapter/Push/APNS.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,14 @@ public function process(PushMessage $message): array
default:
$response->addResultForRecipient(
$device,
self::getSpecificErrorMessage($result['response']['reason'])
$result['response']['reason'] === 'ExpiredToken' ||
$result['response']['reason'] === 'BadDeviceToken' ?
self::getExpiredErrorMessage() : $result['response']['reason'],
);
break;
}
}

return $response->toArray();
}

private static function getSpecificErrorMessage(string $error): string
{
return match ($error) {
'MissingDeviceToken' => 'Bad Request. Missing token.',
'BadDeviceToken' => 'Invalid device token.',
'ExpiredToken' => 'Expired device token.',
'PayloadTooLarge' => 'Payload is too large. Messages must be less than 4096 bytes.',
'TooManyRequests' => 'Too many requests were made to the same device token.',
'InternalServerError' => 'Internal server error.',
'PayloadEmpty' => 'Missing payload.',
default => $error,
};
}
}
18 changes: 14 additions & 4 deletions src/Utopia/Messaging/Adapter/Push/FCM.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,20 @@ protected function process(PushMessage $message): array
if ($result['statusCode'] === 200) {
$response->incrementDeliveredTo();
}
$response->addResultForRecipient(
$message->getTo()[$index],
$result['response']['error']['message'] ?? ''
);

if (isset($result['response']['error'])) {
$response->addResultForRecipient(
$message->getTo()[$index],
$result['response']['error']['status'] === 'UNREGISTERED' ||
$result['response']['error']['status'] === 'INVALID_ARGUMENT'
? self::getExpiredErrorMessage()
: $result['response']['error']['message'] ?? ''
);

continue;
}

$response->addResultForRecipient($message->getTo()[$index]);
}

return $response->toArray();
Expand Down
2 changes: 1 addition & 1 deletion src/Utopia/Messaging/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function getDetails(): array
}

/**
* @return array<string, mixed>
* @return array{deliveredTo: int, type: string, results: array<array<string, mixed>>}
*/
public function toArray(): array
{
Expand Down
Loading