Coder Social home page Coder Social logo

leriomaggio / pyapns Goto Github PK

View Code? Open in Web Editor NEW

This project forked from djacobs/pyapns

0.0 3.0 0.0 90 KB

Python library for interacting with the Apple Push Notification service (APNs)

Home Page: http://pypi.python.org/pypi/apns/

License: MIT License

Python 100.00%

pyapns's Introduction

PyAPNs

A Python library for interacting with the Apple Push Notification service (APNs)

Installation

Either download the source from GitHub or use easy_install:

$ easy_install apns

Sample usage

import time
from apns import APNs, Frame, Payload

apns = APNs(use_sandbox=True, cert_file='cert.pem', key_file='key.pem')

# Send a notification
token_hex = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87'
payload = Payload(alert="Hello World!", sound="default", badge=1)
apns.gateway_server.send_notification(token_hex, payload)

# Send multiple notifications in a single transmission
frame = Frame()
identifier = 1
expiry = time.time()+3600
priority = 10
frame.add_item('b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87', payload, identifier, expiry, priority)
apns.gateway_server.send_notification_multiple(frame)

Apple recommends to query the feedback service daily to get the list of device tokens. You need to create a new connection to APNS to see all the tokens that have failed since you only receive that information upon connection. Remember, once you have viewed the list of tokens, Apple will clear the list from their servers. Use the timestamp to verify that the device tokens haven’t been reregistered since the feedback entry was generated. For each device that has not been reregistered, stop sending notifications. By using this information to stop sending push notifications that will fail to be delivered, you reduce unnecessary message overhead and improve overall system performance.

#New APNS connection
feedback_connection = APNS(use_sandbox=True, cert_file='cert.pem', key_file='key.pem')

# Get feedback messages.
for (token_hex, fail_time) in feedback_connection.feedback_server.items():
    # do stuff with token_hex and fail_time

For more complicated alerts including custom buttons etc, use the PayloadAlert class. Example:

alert = PayloadAlert("Hello world!", action_loc_key="Click me")
payload = Payload(alert=alert, sound="default")

To send custom payload arguments, pass a dictionary to the custom kwarg of the Payload constructor.

payload = Payload(alert="Hello World!", custom={'sekrit_number':123})

Enhanced Message with immediate error-response

apns_enhanced = APNs(use_sandbox=True, cert_file='apns.pem', enhanced=True)

Send a notification. note that identifer is the information to indicate which message has error in error-response payload, it should be UNIQUE since PyAPNs will also use it to determine the range of messages to be re-sent.

token_hex = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87'
payload = Payload(alert="Hello World!", sound="default", badge=1)
identifier = random.getrandbits(32)
apns_enhanced.gateway_server.send_notification(token_hex, payload, identifier=identifier)

Callback when error-response occur, with parameter {'status': <status code from APNS>, 'identifier': <the identifier specified>} Status code reference

def response_listener(error_response):
    _logger.debug("client get error-response: " + str(error_response))

apns_enhanced.gateway_server.register_response_listener(response_listener)

Error response worker will be auto-close after 30 secs idle of connection operations. If you want disable APNS connection and error-responses handlder immediately, force_close it.

apns_enhanced.gateway_server.force_close()

Extra log messages when error-response occur, auto-resent afterwards.

got error-response from APNS:(8, 1)
rebuilding connection to APNS
resending 9 notifications to APNS
resending notification with id:2 to APNS
resending notification with id:3 to APNS
resending notification with id:4 to APNS

Caveats:

  • Currently support single notification only

Problem Addressed (Reference to Redth):

  • Async response of error response and response time varies from 0.1 ~ 0.8 secs by observation
  • Sent success do not response, which means client cannot always expect for response.
  • Async close write stream connection after error-response.
  • All notification sent after failed notification are discarded, the responding error-response and closing client's write connection will be delayed
  • Sometimes APNS close socket connection arbitrary

Solution:

  • Non-blocking ssl socket connection to send notification without waiting for response.
  • A separate thread for constantly checking error-response from read connection.
  • A sent notification buffer used for re-sending notification that were sent after failed notification, or arbitrary connection close by apns.
  • Reference to non-blocking apns pull request by minorblend, enhanced message by hagino3000

Result:

  • Send notification at throughput of 1000/secs
  • In worse case of when 1st notification sent failed, error-response respond after 1 secs and 999 notification sent are discarded by APNS at the mean time, all discarded 999 notifications will be resent without loosing any of them. With the same logic, if notification resent failed, it will resent rest of resent notification after the failed one.

Test

Travis Build Status

Build Status

Further Info

iOS Reference Library: Local and Push Notification Programming Guide

License

PyAPNs is distributed under the terms of the MIT license.

See LICENSE file for the complete license details.

Credits

Written and maintained by Simon Whitaker at Goo Software Ltd.

pyapns's People

Contributors

djacobs avatar jimhorng avatar simonwhitaker avatar neilrahilly avatar zachorr avatar alibitek avatar ogreser avatar mattneary avatar hagino3000 avatar kernys avatar semarj avatar simon-liu avatar xilvar avatar leriomaggio avatar tominsam avatar msabramo avatar co3k avatar yoloseem avatar denya avatar blixt avatar alinik avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.