Add intitial version of APNSPusher #125
This commit is contained in:
parent
cbe3bb428c
commit
5bab600366
2 changed files with 93 additions and 0 deletions
|
|
@ -69,3 +69,8 @@ services:
|
||||||
arguments:
|
arguments:
|
||||||
$ip_address: "%env(MQTT_IP_ADDRESS)%"
|
$ip_address: "%env(MQTT_IP_ADDRESS)%"
|
||||||
$port: "%env(MQTT_PORT)%"
|
$port: "%env(MQTT_PORT)%"
|
||||||
|
|
||||||
|
App\Service\APNSPusher:
|
||||||
|
arguments:
|
||||||
|
$gateway_url: "%env(APNS_GATEWAY_URL)%"
|
||||||
|
$pem_file: "%env(PEM_FILE)%"
|
||||||
|
|
|
||||||
88
src/Service/APNSPusher.php
Normal file
88
src/Service/APNSPusher.php
Normal file
|
|
@ -0,0 +1,88 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Service;
|
||||||
|
|
||||||
|
class APNSPusher
|
||||||
|
{
|
||||||
|
protected $conn;
|
||||||
|
protected $gateway_url;
|
||||||
|
protected $pem_file;
|
||||||
|
protected $is_connected;
|
||||||
|
|
||||||
|
public function __construct($gateway_url, $pem_file)
|
||||||
|
{
|
||||||
|
$this->gateway_url = $gateway_url;
|
||||||
|
$this->pem_file = $pem_file;
|
||||||
|
$this->is_connected = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function connect()
|
||||||
|
{
|
||||||
|
$passphrase = '';
|
||||||
|
$ctx = stream_context_create();
|
||||||
|
stream_context_set_option($ctx, 'ssl', 'local_cert', $this->pem_file);
|
||||||
|
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
|
||||||
|
|
||||||
|
// attempt connection
|
||||||
|
$conn = stream_socket_client($gateway_url, $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
|
||||||
|
|
||||||
|
if (!$conn)
|
||||||
|
{
|
||||||
|
// can't connect to pusher
|
||||||
|
error_log("Failed to connect to APNS: $err $errstr");
|
||||||
|
$this->conn = null;
|
||||||
|
$this->is_connected = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
error_log("Connected to APNS");
|
||||||
|
$this->conn = $conn;
|
||||||
|
$this->is_connected = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function buildPayload($message)
|
||||||
|
{
|
||||||
|
$payload = '{"aps":{"alert":"' . $message . '","sound":"default"}}';
|
||||||
|
return $payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function send($device_id, $payload)
|
||||||
|
{
|
||||||
|
// connect
|
||||||
|
if (!$this->is_connected)
|
||||||
|
$this->connect();
|
||||||
|
|
||||||
|
// build the binary notification
|
||||||
|
$msg = chr(0) . pack('n', 32) . pack('H*', $device_id) . pack('n', strlen($payload)) . $payload;
|
||||||
|
|
||||||
|
// Send it to the server
|
||||||
|
$result = fwrite($this->conn, $msg, strlen($msg));
|
||||||
|
|
||||||
|
if (!$result)
|
||||||
|
{
|
||||||
|
echo 'Undelivered message count: ' . $device_id . '<br />';
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
echo 'Delivered message count: ' . $device_id . '<br />';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->conn)
|
||||||
|
{
|
||||||
|
fclose($this->conn);
|
||||||
|
$this->is_connected = false;
|
||||||
|
error_log("connection closed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected function tr_to_utf($text)
|
||||||
|
{
|
||||||
|
$text = trim($text);
|
||||||
|
$search = array('Ü', 'Þ', 'Ð', 'Ç', 'Ý', 'Ö', 'ü', 'þ', 'ð', 'ç', 'ý', 'ö');
|
||||||
|
$replace = array('Ü', 'Åž', 'Ğž', 'Ç', 'İ', 'Ö', 'ü', 'ÅŸ', 'ÄŸ', 'ç', 'ı', 'ö');
|
||||||
|
$new_text = str_replace($search, $replace, $text);
|
||||||
|
return $new_text;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue