mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-22 05:15:29 +09:00
Clean up FCM v1 payload and response parsing
This commit is contained in:
parent
4f35d82b6d
commit
619eb2a782
1 changed files with 16 additions and 34 deletions
|
|
@ -71,7 +71,7 @@ class FCMv1 extends Base implements PushInterface
|
||||||
|
|
||||||
// Configure Google OAuth2 access token, with appropriate caching.
|
// Configure Google OAuth2 access token, with appropriate caching.
|
||||||
$service_account = Storage::read($this->_config['service_account'] ?? '');
|
$service_account = Storage::read($this->_config['service_account'] ?? '');
|
||||||
$service_account = json_decode($service_account);
|
$service_account = json_decode($service_account, true);
|
||||||
if (!$service_account || empty($service_account['project_id']))
|
if (!$service_account || empty($service_account['project_id']))
|
||||||
{
|
{
|
||||||
$message->addError('FCM error: service account JSON file cannot be decoded');
|
$message->addError('FCM error: service account JSON file cannot be decoded');
|
||||||
|
|
@ -104,13 +104,13 @@ class FCMv1 extends Base implements PushInterface
|
||||||
if (count($metadata))
|
if (count($metadata))
|
||||||
{
|
{
|
||||||
$metadata['sound'] = isset($metadata['sound']) ? $metadata['sound'] : 'default';
|
$metadata['sound'] = isset($metadata['sound']) ? $metadata['sound'] : 'default';
|
||||||
$payload['message']['android'] = $metadata;
|
$payload['message']['android']['notification'] = $metadata;
|
||||||
$payload['message']['webpush'] = $metadata;
|
$payload['message']['webpush']['notification'] = $metadata;
|
||||||
$payload['message']['apns']['payload'] = [
|
$payload['message']['apns']['payload'] = [
|
||||||
'aps' => [
|
'aps' => [
|
||||||
'alert' => ['title' => $title, 'body' => $body],
|
'alert' => ['title' => $title, 'body' => $body],
|
||||||
'sound' => $metadata['sound'],
|
'sound' => $metadata['sound'],
|
||||||
'badge' => $metadata['badge'] ?? '',
|
'badge' => $metadata['badge'] ?? 0,
|
||||||
'category' => $metadata['click_action'] ?? '',
|
'category' => $metadata['click_action'] ?? '',
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
@ -146,44 +146,26 @@ class FCMv1 extends Base implements PushInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
$responses = HTTP::multiple($requests);
|
$responses = HTTP::multiple($requests);
|
||||||
|
|
||||||
// TODO: response parsing
|
|
||||||
foreach ($responses as $response)
|
foreach ($responses as $response)
|
||||||
{
|
{
|
||||||
if ($response->getStatusCode() === 200)
|
$status_code = $response->getStatusCode();
|
||||||
|
$result = @json_decode($response->getBody()->getContents());
|
||||||
|
if ($status_code === 200)
|
||||||
{
|
{
|
||||||
$decoded_response = json_decode($response->getBody());
|
$output->success[$tokens[$i]] = $tokens[$i];
|
||||||
var_dump($response->getStatusCode(), $decoded_response);
|
}
|
||||||
|
elseif ($result && isset($result->error))
|
||||||
if (!$decoded_response)
|
{
|
||||||
|
$error_message = $result->error->message ?? ($result->error->status ?? '');
|
||||||
|
$message->addError('FCM error: HTTP ' . $status_code . ' ' . $error_message);
|
||||||
|
if (str_contains($error_message, 'not a valid FCM registration token'))
|
||||||
{
|
{
|
||||||
$message->addError('FCM error: Invalid Response: '. $response);
|
$output->invalid[$tokens[$i]] = $tokens[$i];
|
||||||
return $output;
|
|
||||||
}
|
|
||||||
$results = $decoded_response->results ?: [];
|
|
||||||
foreach ($results as $result)
|
|
||||||
{
|
|
||||||
if ($result->error)
|
|
||||||
{
|
|
||||||
$message->addError('FCM error: '. $result->error);
|
|
||||||
$output->invalid[$token] = $token;
|
|
||||||
}
|
|
||||||
elseif ($result->message_id && $result->registration_id)
|
|
||||||
{
|
|
||||||
$output->needUpdate[$token] = $result->registration_id;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$output->success[$token] = $result->message_id;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$decoded_response = json_decode($response->getBody());
|
$message->addError('FCM error: HTTP ' . $status_code . ' ' . $response->getReasonPhrase());
|
||||||
var_dump($response->getStatusCode(), $decoded_response);
|
|
||||||
|
|
||||||
$message->addError('FCM error: HTTP ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue