def push(self, device_token = DEVICE_TOKEN): """ send push message to iOS device over APNs""" client = apns2.APNSClient(mode = "dev", client_cert = "certs/apns-dev-cert.pem") alert = apns2.PayloadAlert(body = "some alert content", title = "an alert title") payload = apns2.Payload(alert = alert, content_available = True) notification = apns2.Notification(payload = payload, priority = apns2.PRIORITY_LOW) response = client.push(n = notification, device_token = device_token) print('{0} - {1}, {2}, {3} '.format(response.timestamp response.reason, response.status_code, response.apns_id))
def __init__(self): mode = os.environ.get('APNS_MODE', "dev") cert_file = os.environ.get('APNS_CERT_FILE') certificate_password = os.environ.get('APNS_CERT_PASSWORD', '') def get_password(): return certificate_password self.client = apns2.APNSClient(client_cert=cert_file, mode=mode, password=get_password)
def get_apns() -> apns2.APNSClient: apns = getattr(g, '_apns', None) if apns is None: push_certificate_path = current_app.config['PUSH_CERTIFICATE'] if not os.path.exists(push_certificate_path): raise RuntimeError('You specified a push certificate at: {}, but it does not exist.'.format(push_certificate_path)) client_cert = push_certificate_path # can be a single path or tuple of 2 # We can handle loading PKCS#12 but APNS2Client specifically requests PEM encoded certificates push_certificate_basename, ext = os.path.splitext(push_certificate_path) if ext.lower() == '.p12': pem_key_path = push_certificate_basename + '.key' pem_certificate_path = push_certificate_basename + '.crt' if not os.path.exists(pem_key_path) or not os.path.exists(pem_certificate_path): current_app.logger.info('You provided a PKCS#12 push certificate, we will have to encode it as PEM to continue...') current_app.logger.info('.key and .crt files will be saved in the same location') with open(push_certificate_path, 'rb') as fd: if 'PUSH_CERTIFICATE_PASSWORD' in current_app.config: key, certificate, intermediates = parse_pkcs12(fd.read(), bytes(current_app.config['PUSH_CERTIFICATE_PASSWORD'], 'utf8')) else: key, certificate, intermediates = parse_pkcs12(fd.read()) crypto_key = serialization.load_der_private_key(key.dump(), None, default_backend()) with open(pem_key_path, 'wb') as fd: fd.write(crypto_key.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption())) crypto_cert = x509.load_der_x509_certificate(certificate.dump(), default_backend()) with open(pem_certificate_path, 'wb') as fd: fd.write(crypto_cert.public_bytes(serialization.Encoding.PEM)) client_cert = pem_certificate_path, pem_key_path try: apns = g._apns = apns2.APNSClient(mode='prod', client_cert=client_cert) except: raise RuntimeError('Your push certificate is expired or invalid') return apns
from celery import Celery from celery.schedules import crontab import apns2 import sendgrid from sendgrid.helpers.mail import * import requests from aliyunsms import AliyunSMS import config import json app = Celery('notification', broker="redis://127.0.0.1", backend="redis://127.0.0.1") aps = apns2.APNSClient(mode=config.mode, client_cert=config.apns_cert) sg = sendgrid.SendGridAPIClient(apikey=config.sendgrid_key) app.conf.beat_schedule = { 'notice_check': { 'task': 'tasks.checkNotice', 'schedule': 30.0, 'args': () }, } callback = config.apns_callback @app.task(name='tasks.checkNotice') def checkNotice(): requests.get(config.task_url, timeout=3)