def __init__(self): # apns configuration session = Session() if conf.getboolean('application', 'debug'): con = session.new_connection("feedback_sandbox", cert_file=conf.get('apns', 'cert_sandbox')) else: con = session.new_connection("feedback_production", cert_file=conf.get('apns', 'cert_production')) self.srv = APNs(con)
def __init__(self): # apns configuration self.session = Session() # rabbitmq configuration self.cm = PikaConnectionManager(username=conf.get('rabbitmq', 'username'), password=conf.get('rabbitmq', 'password'), host=conf.get('rabbitmq', 'host'), heartbeat_interval=conf.getint('rabbitmq', 'worker_heartbeat_interval')) self.cm.channel.exchange_declare(exchange='pns_exchange', type='direct', durable=True) self.cm.channel.queue_declare(queue='pns_apns_queue', durable=True) self.cm.channel.queue_bind(exchange='pns_exchange', queue='pns_apns_queue', routing_key='pns_apns') self.cm.channel.basic_qos(prefetch_count=1) self.cm.channel.basic_consume(self._callback, queue='pns_apns_queue')
def setUp(self): self.session = Session(pool="apns_clerk.backends.stdio")
def get_session(self): return Session()
def get_session(self, push=None, feedback=None): backend = DummyBackend(push=push, feedback=feedback) return Session(pool=backend)
def send_apns_message(device, app, message_type, data=None): """ Send an Apple Push Notification message. """ token_list = [device.token] unique_key = device.token if message_type == TYPE_CALL: unique_key = data['unique_key'] message = Message(token_list, payload=get_call_push_payload( unique_key, data['phonenumber'], data['caller_id'])) elif message_type == TYPE_MESSAGE: message = Message(token_list, payload=get_message_push_payload(data['message'])) else: logger.warning('{0} | TRYING TO SENT MESSAGE OF UNKNOWN TYPE: {1}', unique_key, message_type) session = Session() push_mode = settings.APNS_PRODUCTION if device.sandbox: # Sandbox push mode. push_mode = settings.APNS_SANDBOX full_cert_path = os.path.join(settings.CERT_DIR, app.push_key) con = session.get_connection(push_mode, cert_file=full_cert_path) srv = APNs(con) try: logger.info( '{0} | Sending APNS \'{1}\' message at time:{2} to {3} Data:{4}'. format( unique_key, message_type, datetime.datetime.fromtimestamp( time()).strftime('%H:%M:%S.%f'), device.token, data)) res = srv.send(message) except Exception: logger.exception('{0} | Error sending APNS message'.format( unique_key, )) else: # Check failures. Check codes in APNs reference docs. for token, reason in res.failed.items(): code, errmsg = reason # According to APNs protocol the token reported here # is garbage (invalid or empty), stop using and remove it. logger.warning( '{0} | Sending APNS message failed for device: {1}, reason: {2}' .format(unique_key, token, errmsg)) # Check failures not related to devices. for code, errmsg in res.errors: logger.warning('{0} | Error sending APNS message. \'{1}\''.format( unique_key, errmsg)) # Check if there are tokens that can be retried. if res.needs_retry(): logger.info('{0} | Could not sent APNS message, retrying...') # Repeat with retry_message or reschedule your task. res.retry()
import django.utils.timezone as dtz logger = logging.getLogger(__name__) from django.forms.models import model_to_dict from django.contrib.auth import * from service import BaseService import json from apns_clerk import Message, Session, APNs from apns_clerk.apns import Result from apns_clerk.backends.dummy import Backend as DummyBackend from emsg_simple_api.settings import APNS_CERT_PATH, DEBUG ''' emsg_server 回调接口文档 https://github.com/cc14514/emsg_sdk/wiki/emsg_server-%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3 ''' session = Session() # push_sandbox 测试证书 # push_production 生产证书 con = session.get_connection("push_sandbox", cert_file=APNS_CERT_PATH) class user_message(BaseService): def offline(self, body): ''' 离线消息回调,其中 params 是 packet :param body: :return: ''' sn = body['sn'] packet = body['params'] # text image geo audio