Skip to main content

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

Release files for mobi-apns 0.2.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mobi-apns 0.2.9
File Size Uploaded
mobi-apns-0.2.9.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mobi-apns 0.2.9
File Interpreter ABI Platform
mobi_apns-0.2.9-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 15.8 kB

Release files / mobi-apns-0.2.9.tar.gz

Download URL mobi-apns-0.2.9.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
1c87e8f0affd46ee0ad2efad51c4d49108bf8364c931d7c9f1917143105cfdbe
BLAKE2b-256 checksum
How to use checksums
3f555deb84d74f7fed581920f6d42fd1dff4ac4a4e5e0928ba1fde2db7d71a39
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / mobi_apns-0.2.9-py2.py3-none-any.whl

Download URL mobi_apns-0.2.9-py2.py3-none-any.whl
Size 8.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
cb48515eb52e5a32e565d6e7715e4d49489a427ab116a6ebc97484b4dc9bf12c
BLAKE2b-256 checksum
How to use checksums
f9ce1427380c739ccb6228329fdcc42fa72493200759426ae50600bd8e70e8c9
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.2.9 This release

2 release files

0.2.7

2 release files

0.2.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page