Skip to main content

A library for interacting with APNs using HTTP/2 and token-based authentication.

Project description

A library for interacting with APNs using HTTP/2 and token based authentication.

Forked from Gene Sluder’s gobiko.apns (https://github.com/genesluder/python-apns)

Installation

pip install mobi-apns

Requirements

  • Python 3.5+

  • hyper

  • pyjwt

  • cryptography

Usage

Create a client:

from mobi.apns import APNsClient, APNsMessage

client = APNsClient(
    team_id=TEAM_ID,
    bundle_id=BUNDLE_ID,
    auth_key_id=APNS_KEY_ID,
    auth_key_filepath=APNS_KEY_FILEPATH,
    use_sandbox=True
)

Alternatively, you can create a client with the contents of the auth key file directly:

client = APNsClient(
    team_id=TEAM_ID,
    bundle_id=BUNDLE_ID,
    auth_key_id=APNS_KEY_ID,
    auth_key=APNS_KEY,
    use_sandbox=True
)

If you run into any problems deserializing the key, try wrapping it to 64 lines:

client = APNsClient(
    team_id=TEAM_ID,
    bundle_id=BUNDLE_ID,
    auth_key_id=APNS_KEY_ID,
    auth_key=APNS_KEY,
    use_sandbox=True,
    wrap_key=True
)

Construct a message:

message = APNsMessage('TEST',
           badge=1,
           sound='default',
           loc_key='This is a %@',
           loc_args=['Test'])

Now you can send a message to a device by specifying its registration ID:

client.send_message(
    registration_id,
    message
)

Or you can send bulk messages to a list of devices:

client.send_bulk_message(
    [registration_id_1, registration_id_2],
    message
)

Payload

Additional APNs payload values can be passed into the message construction

message = APNsMessage(
    "All your base are belong to us.",
    badge=None,
    sound=None,
    category=None,
    content_available=False,
    action_loc_key=None,
    loc_key=None,
    loc_args=[],
    extra={},
    identifier=None,
    expiration=None,
    priority=10,
    topic=None
)

Pruning

The legacy binary interface APNs provided an endpoint to check whether a registration ID had become inactive. Now the service returns a BadDeviceToken error when you attempt to deliver an alert to an inactive registration ID. If you need to prune inactive IDs from a database you can handle the BadDeviceToken exception to do so:

::

from mobi.apns.exceptions import BadDeviceToken

try:

client.send_message(OLD_REGISTRATION_ID, “Message to an invalid registration ID.”)

except BadDeviceToken:

# Handle invalid ID here pass

Same approach if sending by bulk:

::

from mobi.apns.exceptions import PartialBulkMessage

try:

client.send_bulk_message([registration_id1, registration_id2], “Message”)

except PartialBulkMessage as e:

# Handle list of invalid IDs using e.bad_registration_ids pass

Documentation

  • More information on APNs and an explanation of the above can be found in this blog post <http://gobiko.com/blog/token-based-authentication-http2-example-apns/>_.

  • Apple documentation for APNs can be found here <https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/APNSOverview.html#//apple_ref/doc/uid/TP40008194-CH8-SW1>_.

Credits

  • Gene Sluder

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mobi-apns-0.2.9.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

mobi_apns-0.2.9-py2.py3-none-any.whl (8.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file mobi-apns-0.2.9.tar.gz.

File metadata

  • Download URL: mobi-apns-0.2.9.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

File hashes

Hashes for mobi-apns-0.2.9.tar.gz
Algorithm Hash digest
SHA256 1c87e8f0affd46ee0ad2efad51c4d49108bf8364c931d7c9f1917143105cfdbe
MD5 dd5491436743357cc1a176ac69935a36
BLAKE2b-256 3f555deb84d74f7fed581920f6d42fd1dff4ac4a4e5e0928ba1fde2db7d71a39

See more details on using hashes here.

File details

Details for the file mobi_apns-0.2.9-py2.py3-none-any.whl.

File metadata

  • Download URL: mobi_apns-0.2.9-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

File hashes

Hashes for mobi_apns-0.2.9-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 cb48515eb52e5a32e565d6e7715e4d49489a427ab116a6ebc97484b4dc9bf12c
MD5 bbb2bc4a5ca78b423f6d17c0d93f8b5e
BLAKE2b-256 f9ce1427380c739ccb6228329fdcc42fa72493200759426ae50600bd8e70e8c9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page