Fix #1933 enable sending FCM message without the "notification" field

This commit is contained in:
Kijin Sung 2023-08-03 22:44:27 +09:00
parent fc932747e9
commit 00159407f4

View file

@ -2,10 +2,14 @@
namespace Rhymix\Framework\Drivers\Push; namespace Rhymix\Framework\Drivers\Push;
use Rhymix\Framework\HTTP;
use Rhymix\Framework\Push;
use Rhymix\Framework\Drivers\PushInterface;
/** /**
* The FCM (Google) Push driver. * The FCM (Google) Push driver.
*/ */
class FCM extends Base implements \Rhymix\Framework\Drivers\PushInterface class FCM extends Base implements PushInterface
{ {
/** /**
* Config keys used by this driver are stored here. * Config keys used by this driver are stored here.
@ -44,7 +48,7 @@ class FCM extends Base implements \Rhymix\Framework\Drivers\PushInterface
* @param array $tokens * @param array $tokens
* @return \stdClass * @return \stdClass
*/ */
public function send(\Rhymix\Framework\Push $message, array $tokens): \stdClass public function send(Push $message, array $tokens): \stdClass
{ {
$output = new \stdClass; $output = new \stdClass;
$output->success = []; $output->success = [];
@ -60,27 +64,38 @@ class FCM extends Base implements \Rhymix\Framework\Drivers\PushInterface
// Set notification // Set notification
$notification = $message->getMetadata(); $notification = $message->getMetadata();
$notification['title'] = $message->getSubject(); $subject = $message->getSubject();
$notification['body'] = $message->getContent(); $content = $message->getContent();
$notification['sound'] = isset($notification['sound']) ? $notification['sound'] : 'default'; if ($subject !== '' || $content !== '')
{
$notification['title'] = $subject;
$notification['body'] = $content;
}
if (count($notification))
{
$notification['sound'] = isset($notification['sound']) ? $notification['sound'] : 'default';
}
$chunked_token = array_chunk($tokens, 1000); $chunked_token = array_chunk($tokens, 1000);
foreach($chunked_token as $token_unit) foreach($chunked_token as $token_unit)
{ {
$data = json_encode(array( $data = [
'registration_ids' => $token_unit, 'registration_ids' => $token_unit,
'notification' => $notification,
'priority' => 'normal', 'priority' => 'normal',
'data' => $message->getData() ?: new \stdClass, 'data' => $message->getData() ?: new \stdClass,
)); ];
if (count($notification))
$response = \FileHandler::getRemoteResource($url, $data, 5, 'POST', 'application/json', $headers);
if($response)
{ {
$decoded_response = json_decode($response); $data['notification'] = $notification;
}
$response = HTTP::request($url, 'POST', json_encode($data), $headers);
if($response->getStatusCode() === 200)
{
$decoded_response = json_decode($response->getBody());
if(!$decoded_response) if(!$decoded_response)
{ {
$message->addError('FCM return invalid json : '. $response); $message->addError('FCM error: Invalid Response: '. $response);
return $output; return $output;
} }
$results = $decoded_response->results ?: []; $results = $decoded_response->results ?: [];
@ -88,7 +103,7 @@ class FCM extends Base implements \Rhymix\Framework\Drivers\PushInterface
{ {
if($result->error) if($result->error)
{ {
$message->addError('FCM error code: '. $result->error); $message->addError('FCM error: '. $result->error);
$output->invalid[$token_unit[$i]] = $token_unit[$i]; $output->invalid[$token_unit[$i]] = $token_unit[$i];
} }
else if($result->message_id && $result->registration_id) else if($result->message_id && $result->registration_id)
@ -103,7 +118,7 @@ class FCM extends Base implements \Rhymix\Framework\Drivers\PushInterface
} }
else else
{ {
$message->addError('FCM return empty response.'); $message->addError('FCM error: HTTP ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase());
} }
} }
return $output; return $output;