rhymix/common/framework/drivers/push/apns.php
2020-06-19 16:01:30 +09:00

89 lines
2.1 KiB
PHP

<?php
namespace Rhymix\Framework\Drivers\Push;
/**
* The APNs (Apple) Push driver.
*/
class APNs extends Base implements \Rhymix\Framework\Drivers\PushInterface
{
/**
* Config keys used by this driver are stored here.
*/
protected static $_required_config = array('certificate', 'passphrase');
protected static $_optional_config = array();
/**
* Get the human-readable name of this Push driver.
*
* @return string
*/
public static function getName(): string
{
return 'iOS (APNs)';
}
/**
* Check if the current Push driver is supported on this server.
*
* This method returns true on success and false on failure.
*
* @return bool
*/
public static function isSupported(): bool
{
return true;
}
/**
* Send a message.
*
* This method returns true on success and false on failure.
*
* @param object $message
* @param array $tokens
* @return bool
*/
public function send(\Rhymix\Framework\Push $message, array $tokens): bool
{
$status = true;
// Set parameters
$local_cert = $this->_config['certificate'];
$passphrase = $this->_config['passphrase'];
$alert = [];
$alert['title'] = $message->getSubject();
$alert['body'] = $message->getContent();
if($message->getImage())
{
$alert['image'] = $message->getImage();
}
$body['aps'] = array('alert' => $alert,'sound' => 'default');
$payload = json_encode($body);
foreach($tokens as $token)
{
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $local_cert);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 5, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
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;
}
fclose($fp);
}
return $status;
}
}