aioapns: Asynchronous APNs Client

4.0 · active · verified Tue Apr 14

aioapns is an efficient and asynchronous APNs (Apple Push Notification service) client library for Python, built on asyncio. It allows developers to send push notifications to iOS, macOS, watchOS, and tvOS devices. The current version is 4.0, and it generally follows a moderate release cadence, with major versions introducing significant changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the APNs client and send a simple push notification using an asynchronous context. It requires your APNs authentication key content, key ID, team ID, app bundle ID, and a device token. Remember to use the `use_sandbox=False` for production environments.

import asyncio
import os
from aioapns import APNs, NotificationRequest

async def main():
    # Ensure you have these environment variables set or replace with actual values
    # APNS_KEY_CONTENT: The content of your .p8 private key file (NOT the path)
    # APNS_KEY_ID: Your 10-character key ID from Apple Developer website
    # APNS_TEAM_ID: Your 10-character Team ID from Apple Developer website
    # APNS_TOPIC: Your app's bundle ID (e.g., com.example.app)
    # APNS_DEVICE_TOKEN: A valid device token obtained from an iOS/macOS device

    key_content = os.environ.get('APNS_KEY_CONTENT', '-----BEGIN PRIVATE KEY-----\nYOUR_P8_KEY_CONTENT_HERE\n-----END PRIVATE KEY-----')
    key_id = os.environ.get('APNS_KEY_ID', 'YOUR_KEY_ID')
    team_id = os.environ.get('APNS_TEAM_ID', 'YOUR_TEAM_ID')
    topic = os.environ.get('APNS_TOPIC', 'com.example.app')
    device_id = os.environ.get('APNS_DEVICE_TOKEN', 'your_device_token_here')

    if 'YOUR_P8_KEY_CONTENT_HERE' in key_content or 'YOUR_KEY_ID' == key_id or 'YOUR_TEAM_ID' == team_id or 'com.example.app' == topic or 'your_device_token_here' == device_id:
        print("Please set APNS_KEY_CONTENT, APNS_KEY_ID, APNS_TEAM_ID, APNS_TOPIC, and APNS_DEVICE_TOKEN environment variables or replace placeholders in the code.")
        return

    try:
        apns_client = APNs(
            key=key_content,  # For v4.0+, this is the key content (string), not a path
            key_id=key_id,
            team_id=team_id,
            topic=topic,
            use_sandbox=True, # Set to False for production environment
        )

        request = NotificationRequest(
            device_id=device_id,
            message={
                'aps': {
                    'alert': 'Hello from aioapns!',
                    'sound': 'default',
                    'badge': 1
                },
                'custom_data': 'some_value'
            }
        )

        response = await apns_client.send_notification(request)

        if response.is_successful:
            print(f"Notification successfully sent to {device_id}!")
        else:
            print(f"Failed to send notification. Status: {response.status}, Reason: {response.reason}")

    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == '__main__':
    asyncio.run(main())

view raw JSON →