Replace apns sender with new one #196
This commit is contained in:
parent
b09ca589f2
commit
d7a2e7f899
5 changed files with 150 additions and 1 deletions
40
src/Command/TestRegAPNSCommand.php
Normal file
40
src/Command/TestRegAPNSCommand.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
|
||||
use App\Service\APNSClient;
|
||||
|
||||
use DateTime;
|
||||
|
||||
class TestRegAPNSCommand extends Command
|
||||
{
|
||||
protected $apns;
|
||||
|
||||
public function __construct( APNSClient $apns)
|
||||
{
|
||||
$this->apns = $apns;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('apns:test_reg')
|
||||
->setDescription('Test new format for Apple Push Notification.')
|
||||
->setHelp('Test new format for Apple Push Notification.')
|
||||
->addArgument('push_id', InputArgument::REQUIRED, 'push_id');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$push_id = $input->getArgument('push_id');
|
||||
$this->apns->sendReg($push_id, 'Test Delay', 'Body');
|
||||
}
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@ use Redis;
|
|||
|
||||
class APNSClient
|
||||
{
|
||||
// TODO: make this a config entry
|
||||
const REDIS_KEY = 'apns_push';
|
||||
|
||||
// protected $mclient;
|
||||
|
|
@ -53,4 +54,9 @@ class APNSClient
|
|||
$this->push($push_id, $message);
|
||||
}
|
||||
}
|
||||
|
||||
public function sendReg($push_id, $message)
|
||||
{
|
||||
$this->push($push_id, $message);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ class APNSClient():
|
|||
def setupConnection(self):
|
||||
# socket
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
wsock = ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_TLSv1, certfile=self.cert_file)
|
||||
wsock = ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_TLSv1_2, certfile=self.cert_file)
|
||||
wsock.connect((self.host, self.port))
|
||||
|
||||
self.is_connected = True
|
||||
|
|
|
|||
90
utils/apns_sender/new_sender.py
Normal file
90
utils/apns_sender/new_sender.py
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
from daemonize import Daemonize
|
||||
from apns import APNs, Frame, Payload
|
||||
import redis
|
||||
import time
|
||||
import signal
|
||||
import sys
|
||||
import os
|
||||
import logging
|
||||
import socket
|
||||
|
||||
import ssl
|
||||
import struct
|
||||
import binascii
|
||||
|
||||
|
||||
|
||||
def sigint_handler(signal, frame):
|
||||
sys.exit(0)
|
||||
os._exit(0)
|
||||
|
||||
def redisLoop(apns, logger):
|
||||
r = redis.StrictRedis(host='localhost', port=6379, db=0)
|
||||
while 1:
|
||||
#logger.info("Checking queue for messages to send...")
|
||||
time.sleep(0)
|
||||
data = r.brpop("apns_push", 10)
|
||||
if data:
|
||||
info = data[1].split('|')
|
||||
logger.info("Token: " + info[0] + " message: " + info[1])
|
||||
try:
|
||||
apns.sendNotification(info[0], info[1], "notification")
|
||||
except TypeError:
|
||||
logger.info("Invalid token: " + info[0])
|
||||
|
||||
|
||||
def sigint_handler(signal, frame):
|
||||
sys.exit(0)
|
||||
|
||||
def get_logger():
|
||||
logger = logging.getLogger("apns_logger")
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
fh = logging.FileHandler("/tmp/apns_sender.log")
|
||||
fmt = '%(asctime)s - %(threadName)s - %(levelname)s - %(message)s'
|
||||
formatter = logging.Formatter(fmt)
|
||||
fh.setFormatter(formatter)
|
||||
|
||||
logger.addHandler(fh)
|
||||
return logger
|
||||
|
||||
class APNSClient():
|
||||
def __init__(self, title, cert_file, logger):
|
||||
self.title = title
|
||||
self.cert_file = cert_file
|
||||
self.logger = logger
|
||||
# connect
|
||||
self.apns = APNs(cert_file=self.cert_file)
|
||||
|
||||
def sendNotification(self, token, body, notif_type):
|
||||
# apns packet
|
||||
alert = {"title" : self.title,
|
||||
"body" : body,
|
||||
"type" : notif_type}
|
||||
|
||||
payload = Payload(alert=alert, sound="default", badge=1, content_available=True)
|
||||
self.apns.gateway_server.send_notification(token, payload)
|
||||
|
||||
|
||||
def main():
|
||||
logger = get_logger()
|
||||
logger.info("Starting apns_sender")
|
||||
|
||||
#apns = APNs(use_sandbox=False, cert_file=cert_file)
|
||||
#apns = setup_apns()
|
||||
|
||||
# TODO: load from config file
|
||||
cert_file = "/root/ios_push_test/ios_prod.pem"
|
||||
apns = APNSClient("Motolite Res-q", cert_file, logger)
|
||||
|
||||
logger.info("Starting redis loop")
|
||||
redisLoop(apns, logger)
|
||||
|
||||
signal.signal(signal.SIGINT, sigint_handler)
|
||||
|
||||
|
||||
|
||||
pid = "/tmp/apns_sender.pid"
|
||||
#daemon = Daemonize(app="apns_sender", pid=pid, action=main)
|
||||
#daemon.start()
|
||||
main()
|
||||
13
utils/test_apns/test_apns.py
Normal file
13
utils/test_apns/test_apns.py
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
from apns import APNs, Frame, Payload
|
||||
|
||||
|
||||
#apns = APNs(use_sandbox=True, cert_file='/root/ios_push_test/motoliteUserDev.pem')
|
||||
apns = APNs(cert_file='/root/ios_push_test/motoliteUserProd.pem')
|
||||
|
||||
alert = {"title" : "Motolite Res-q Notification",
|
||||
"body" : "Test Notification",
|
||||
"type" : "register"}
|
||||
|
||||
token_hex = "0D89F702B8E5A235E5100C47912DCBC346CE503DBB860572402031595D6F4C6C"
|
||||
payload = Payload(alert=alert, sound="default", badge=1, content_available=True)
|
||||
apns.gateway_server.send_notification(token_hex, payload)
|
||||
Loading…
Reference in a new issue