mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-02 16:52:16 +09:00
Allow sending message to topics using FCM v1 API
This commit is contained in:
parent
ac16d2e7f5
commit
c7f96ad75b
2 changed files with 64 additions and 8 deletions
|
|
@ -123,8 +123,8 @@ class FCMv1 extends Base implements PushInterface
|
|||
}
|
||||
|
||||
// Send a notification to each token, grouped into chunks to speed up the process.
|
||||
$chunked_tokens = array_chunk($tokens, self::CHUNK_SIZE);
|
||||
foreach($chunked_tokens as $tokens)
|
||||
$chunked_tokens = $tokens ? array_chunk($tokens, self::CHUNK_SIZE) : [[]];
|
||||
foreach ($chunked_tokens as $tokens)
|
||||
{
|
||||
$requests = [];
|
||||
foreach ($tokens as $i => $token)
|
||||
|
|
@ -132,9 +132,6 @@ class FCMv1 extends Base implements PushInterface
|
|||
$requests[$i] = [
|
||||
'url' => $api_url,
|
||||
'method' => 'POST',
|
||||
'data' => null,
|
||||
'headers' => [],
|
||||
'cookies' => [],
|
||||
'settings' => [
|
||||
'auth' => 'google_auth',
|
||||
'base_uri' => self::BASE_URL,
|
||||
|
|
@ -170,6 +167,43 @@ class FCMv1 extends Base implements PushInterface
|
|||
}
|
||||
}
|
||||
|
||||
// Send a notification to each topic.
|
||||
$topics = $message->getTopics();
|
||||
if (count($topics))
|
||||
{
|
||||
$requests = [];
|
||||
foreach ($topics as $i => $topic)
|
||||
{
|
||||
$requests[$i] = [
|
||||
'url' => $api_url,
|
||||
'method' => 'POST',
|
||||
'settings' => [
|
||||
'auth' => 'google_auth',
|
||||
'base_uri' => self::BASE_URL,
|
||||
'handler' => $stack,
|
||||
'json' => $payload,
|
||||
],
|
||||
];
|
||||
$requests[$i]['settings']['json']['message']['topic'] = $topic;
|
||||
}
|
||||
|
||||
$responses = HTTP::multiple($requests);
|
||||
foreach ($responses as $response)
|
||||
{
|
||||
$status_code = $response->getStatusCode();
|
||||
$result = @json_decode($response->getBody()->getContents());
|
||||
if ($status_code === 200)
|
||||
{
|
||||
$output->success[$topics[$i]] = $result->name ?? '';
|
||||
}
|
||||
else
|
||||
{
|
||||
$error_message = $result->error->message ?? ($result->error->status ?? $response->getReasonPhrase());
|
||||
$message->addError('FCM error: HTTP ' . $status_code . ' ' . $error_message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue