mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-09 11:44:10 +09:00
알림 기능 구현FCM은 테스트 완료
This commit is contained in:
parent
07e28b4ef9
commit
347152e163
9 changed files with 127 additions and 55 deletions
|
|
@ -42,11 +42,14 @@ class APNs extends Base implements \Rhymix\Framework\Drivers\PushInterface
|
|||
*
|
||||
* @param object $message
|
||||
* @param array $tokens
|
||||
* @return bool
|
||||
* @return object
|
||||
*/
|
||||
public function send(\Rhymix\Framework\Push $message, array $tokens): bool
|
||||
public function send(\Rhymix\Framework\Push $message, array $tokens)
|
||||
{
|
||||
$status = true;
|
||||
$output = new \stdClass;
|
||||
$output->success = [];
|
||||
$output->invalid = [];
|
||||
$output->needUpdate = [];
|
||||
|
||||
// Set parameters
|
||||
$local_cert = $this->_config['certificate'];
|
||||
|
|
@ -68,18 +71,17 @@ class APNs extends Base implements \Rhymix\Framework\Drivers\PushInterface
|
|||
if(!$fp)
|
||||
{
|
||||
$message->addError('Failed to connect socket - error code: '. $err .' - '. $errstr);
|
||||
$status = false;
|
||||
}
|
||||
$msg = chr(0) . pack('n', 32) . pack('H*', $token) . pack('n', strlen($payload)) . $payload;
|
||||
$result = fwrite($fp, $msg, strlen($msg));
|
||||
if(!$result)
|
||||
{
|
||||
$message->addError('APNs return empty response.');
|
||||
$status = false;
|
||||
}
|
||||
$output->success[] = $token;
|
||||
fclose($fp);
|
||||
}
|
||||
|
||||
return $status;
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Rhymix\Framework\Drivers\Push;
|
||||
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* The base class for other Push drivers.
|
||||
*/
|
||||
|
|
@ -86,10 +88,10 @@ abstract class Base implements \Rhymix\Framework\Drivers\PushInterface
|
|||
*
|
||||
* @param object $message
|
||||
* @param array $tokens
|
||||
* @return bool
|
||||
* @return object
|
||||
*/
|
||||
public function send(\Rhymix\Framework\Push $message, array $tokens): bool
|
||||
public function send(\Rhymix\Framework\Push $message, array $tokens)
|
||||
{
|
||||
return false;
|
||||
return new \stdClass;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,6 @@
|
|||
|
||||
namespace Rhymix\Framework\Drivers\Push;
|
||||
|
||||
use message;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* The FCM (Google) Push driver.
|
||||
*/
|
||||
|
|
@ -45,11 +42,14 @@ class FCM extends Base implements \Rhymix\Framework\Drivers\PushInterface
|
|||
*
|
||||
* @param object $message
|
||||
* @param array $tokens
|
||||
* @return bool
|
||||
* @return object
|
||||
*/
|
||||
public function send(\Rhymix\Framework\Push $message, array $tokens): bool
|
||||
public function send(\Rhymix\Framework\Push $message, array $tokens)
|
||||
{
|
||||
$status = true;
|
||||
$output = new \stdClass;
|
||||
$output->success = [];
|
||||
$output->invalid = [];
|
||||
$output->needUpdate = [];
|
||||
|
||||
$url = 'https://fcm.googleapis.com/fcm/send';
|
||||
$api_key = $this->_config['api_key'];
|
||||
|
|
@ -64,31 +64,48 @@ class FCM extends Base implements \Rhymix\Framework\Drivers\PushInterface
|
|||
$notification['body'] = $message->getContent();
|
||||
$notification['click_action'] = $message->getClickAction();
|
||||
|
||||
foreach($tokens as $i => $token)
|
||||
$chunked_token = array_chunk($tokens, 1000);
|
||||
foreach($chunked_token as $token_unit)
|
||||
{
|
||||
$data = json_encode(array(
|
||||
'registration_ids' => [$token],
|
||||
'registration_ids' => $token_unit,
|
||||
'notification' => $notification,
|
||||
'priority' => 'normal',
|
||||
'data' => $message->getData() ?: new stdClass,
|
||||
'data' => $message->getData() ?: new \stdClass,
|
||||
));
|
||||
|
||||
$result = \FileHandler::getRemoteResource($url, $data, 5, 'POST', 'application/json', $headers);
|
||||
if($result)
|
||||
$response = \FileHandler::getRemoteResource($url, $data, 5, 'POST', 'application/json', $headers);
|
||||
if($response)
|
||||
{
|
||||
$error = json_decode($result)->error_code;
|
||||
if($error)
|
||||
$decoded_response = json_decode($response);
|
||||
if(!$decoded_response)
|
||||
{
|
||||
$message->addError('FCM error code: '. $error);
|
||||
$status = false;
|
||||
$message->addError('FCM return invalid json : '. $response);
|
||||
return $output;
|
||||
}
|
||||
$results = $decoded_response->results ?: [];
|
||||
foreach($results as $i => $result)
|
||||
{
|
||||
if($result->error)
|
||||
{
|
||||
$message->addError('FCM error code: '. $result->error);
|
||||
$output->invalid[$token_unit[$i]] = $token_unit[$i];
|
||||
}
|
||||
else if($result->message_id && $result->registration_id)
|
||||
{
|
||||
$output->needUpdate[$token_unit[$i]] = $result->registration_id;
|
||||
}
|
||||
else
|
||||
{
|
||||
$output->success[$token_unit[$i]] = $result->message_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$message->addError('FCM return empty response.');
|
||||
$status = false;
|
||||
}
|
||||
}
|
||||
return $status;
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ interface PushInterface
|
|||
*
|
||||
* @param object $message
|
||||
* @param array $tokens
|
||||
* @return bool
|
||||
* @return object
|
||||
*/
|
||||
public function send(\Rhymix\Framework\Push $message, array $tokens): bool;
|
||||
public function send(\Rhymix\Framework\Push $message, array $tokens);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,6 @@
|
|||
|
||||
namespace Rhymix\Framework;
|
||||
|
||||
use BaseObject;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* The Push class.
|
||||
*/
|
||||
|
|
@ -16,12 +13,12 @@ class Push
|
|||
protected $from = 0;
|
||||
protected $to = array();
|
||||
protected $subject = '';
|
||||
protected $content = '';
|
||||
protected $content = '';
|
||||
protected $click_action = '';
|
||||
protected $data = [];
|
||||
protected $errors = array();
|
||||
protected $sent = false;
|
||||
|
||||
|
||||
/**
|
||||
* Static properties.
|
||||
*/
|
||||
|
|
@ -286,19 +283,25 @@ class Push
|
|||
|
||||
try
|
||||
{
|
||||
$tokens = $this->getDeviceTokens();
|
||||
$tokens = $this->_getDeviceTokens();
|
||||
// Android FCM
|
||||
if(count($tokens['android']))
|
||||
if(count($tokens->android))
|
||||
{
|
||||
$fcm_driver = $this->getDriver('fcm');
|
||||
$this->sent = $fcm_driver->send($this, $tokens['android']);
|
||||
$output = $fcm_driver->send($this, $tokens->android);
|
||||
$this->sent = $output->invalid ? false : true;
|
||||
$this->_deleteInvalidTokens($output->invalid);
|
||||
$this->_updateDeviceTokens($output->needUpdate);
|
||||
}
|
||||
|
||||
// iOS APNs
|
||||
if(count($tokens['ios']))
|
||||
if(count($tokens->ios))
|
||||
{
|
||||
$apns_driver =$this->getDriver('apns');
|
||||
$this->sent = $apns_driver->send($this, $tokens['ios']);
|
||||
$output = $apns_driver->send($this, $tokens->ios);
|
||||
$this->sent = $output->invalid ? false : true;
|
||||
$this->_deleteInvalidTokens($output->invalid);
|
||||
$this->_updateDeviceTokens($output->needUpdate);
|
||||
}
|
||||
}
|
||||
catch(\Exception $e)
|
||||
|
|
@ -319,21 +322,21 @@ class Push
|
|||
/**
|
||||
* Get the device token
|
||||
*
|
||||
* @return array
|
||||
* @return object
|
||||
*
|
||||
*/
|
||||
protected function getDeviceTokens(): array
|
||||
protected function _getDeviceTokens()
|
||||
{
|
||||
$member_srl_list = $this->getRecipients();
|
||||
$result = [];
|
||||
$result['android'] = [];
|
||||
$result['ios'] = [];
|
||||
$result = new \stdClass;
|
||||
$result->android = [];
|
||||
$result->ios = [];
|
||||
|
||||
$args = new stdClass;
|
||||
$args = new \stdClass;
|
||||
$args->member_srl = $member_srl_list;
|
||||
$args->device_type = [];
|
||||
$driver_types = config('push.types') ?: array();
|
||||
if(count($driver_types))
|
||||
if(!count($driver_types))
|
||||
{
|
||||
return $result;
|
||||
}
|
||||
|
|
@ -354,12 +357,44 @@ class Push
|
|||
|
||||
foreach($output->data as $row)
|
||||
{
|
||||
$result[$row->device_type][] = $row->device_token;
|
||||
$result->{$row->device_type}[] = $row->device_token;
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the device toekn
|
||||
*
|
||||
* @param array
|
||||
*/
|
||||
protected function _deleteInvalidTokens(array $invalid_tokens): void
|
||||
{
|
||||
if(!count($invalid_tokens))
|
||||
{
|
||||
return;
|
||||
}
|
||||
$args = new \stdClass;
|
||||
$args->device_token = $invalid_tokens;
|
||||
executeQueryArray('member.deleteMemberDevice', $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the device toekn
|
||||
*
|
||||
* @param array
|
||||
*/
|
||||
protected function _updateDeviceTokens(array $update_tokens): void
|
||||
{
|
||||
$args = new \stdClass;
|
||||
foreach($update_tokens as $key => $value)
|
||||
{
|
||||
$args->old_token = $key;
|
||||
$args->new_token = $value;
|
||||
executeQueryArray('member.updateMemberDevice', $args);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the message was sent.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue