Skip to main content

Synchronous Python client for Apple Push Notification Service

Project description

Simple APNS

Tests PyPI version Python Versions License: MIT

A synchronous Python client for Apple Push Notification Service (APNS) that integrates easily with Django. The library uses httpx for HTTP/2 requests.

Features

  • Synchronous client for APNS
  • JWT authentication support
  • Convenient notification builder
  • Built-in Django integration
  • Send both single and bulk notifications
  • Automatic retry attempts with delay
  • Full support for all notification types (alert, background, location, voip, etc.)

Installation

Basic Installation

pip install simple-apns

Installation with Django Support

pip install simple-apns[django]

Usage

Basic Usage

from simple_apns import APNSClient, Payload

# Initialize the client
client = APNSClient(
    team_id="ABCDE12345",  # Your Apple Developer Team ID
    auth_key_id="ABC123DEFG",  # Authentication Key ID
    auth_key_path="/path/to/AuthKey_ABC123DEFG.p8",  # Path to the key file
    bundle_id="com.example.app",  # Your app's bundle ID
    use_sandbox=True,  # Use development environment
)

# Create a simple notification
payload = Payload(
    alert_title="Notification Title", 
    alert_body="Notification Text"
)

# Add custom data
payload.add_custom_data("user_id", "12345")

# Send the notification
try:
    success = client.send_notification(
        device_token="<device_token>",
        payload=payload
    )
    print(f"Notification sent: {success}")
except Exception as e:
    print(f"Error sending notification: {e}")

# Don't forget to close the connection
client.close()

Creating Advanced Notifications

from simple_apns import Payload

payload = Payload()

# Configure notification content
payload.set_alert(
    title="New Message",
    body="You have a new message from Anna",
    subtitle="Chat"
)

# Add sound
payload.set_sound("default")

# Set badge
payload.set_badge(5)

# Category for custom actions
payload.set_category("MESSAGE")

# Group notifications
payload.set_thread_id("chat-123")

# For background notifications
payload.set_content_available(True)

# For notification extensions
payload.set_mutable_content(True)

# Add custom data
payload.add_custom_data("message_id", "m-123")
payload.add_custom_data("sender_id", "user-456")

Sending Bulk Notifications

from simple_apns import APNSClient, Payload

client = APNSClient(
    team_id="ABCDE12345",
    auth_key_id="ABC123DEFG",
    auth_key_path="/path/to/AuthKey_ABC123DEFG.p8",
    bundle_id="com.example.app",
)

device_tokens = [
    "token1",
    "token2",
    "token3",
]

payload = Payload(
    alert_title="Bulk Notification", 
    alert_body="Sent to all users"
)

results = client.send_bulk_notifications(
    device_tokens=device_tokens,
    payload=payload
)

for token, success in results.items():
    print(f"Token {token}: {'success' if success else 'failed'}")

client.close()

Django Integration

Configuration

  1. Add simple_apns.django to INSTALLED_APPS in your settings.py file:
INSTALLED_APPS = [
    # ...
    'simple_apns.django',
    # ...
]
  1. Add APNS configuration to your settings.py file:
SIMPLE_APNS = {
    'TEAM_ID': 'ABCDE12345',
    'AUTH_KEY_ID': 'ABC123DEFG',
    'AUTH_KEY_PATH': '/path/to/AuthKey_ABC123DEFG.p8',
    'BUNDLE_ID': 'com.example.app',
    'USE_SANDBOX': True,  # Optional, default is False
    'APNS_TOPIC': None,  # Optional, default is BUNDLE_ID
    'TIMEOUT': 10,  # Optional, default is 10
    'MAX_RETRIES': 3,  # Optional, default is 3
}

Using in Django

from simple_apns.django import send_notification, send_bulk_notifications

# Send a single notification
try:
    success = send_notification(
        device_token="<device_token>",
        title="Title",
        body="Notification text",
        badge=1,
        sound="default",
        extra_data={"key": "value"}
    )
    print(f"Notification sent: {success}")
except Exception as e:
    print(f"Error sending notification: {e}")

# Send bulk notifications
tokens = ["token1", "token2", "token3"]
results = send_bulk_notifications(
    device_tokens=tokens,
    title="Bulk Notification",
    body="Sent to all users",
    extra_data={"campaign_id": "123"}
)

Accessing the Client Directly

from simple_apns.django import get_apns_client
from simple_apns import Payload

# Get the configured client
client = get_apns_client()

# Create a custom notification
payload = Payload()
payload.set_alert(title="Custom Notification", body="Text")
payload.set_content_available(True)

# Send
client.send_notification(
    device_token="<device_token>",
    payload=payload,
    push_type="background",
    priority=5
)

Push Notification Types

Apple supports several types of push notifications, specified in the push_type parameter:

  • alert (default): Standard notification with visual display
  • background: Background notification for data updates without visual display
  • voip: Notification for VoIP calls (requires special permissions)
  • complication: Notification for watchOS complications updates
  • fileprovider: Notification for File Provider Extension
  • mdm: Notification for Mobile Device Management
  • location: Notification for LocationPush

Notification Priority

For the priority parameter, you can specify:

  • 10 (default): Immediate notification delivery
  • 5: Delivery with power consumption optimization (for background notifications)

Security

To ensure security, make sure that:

  1. The authentication key file (.p8) is stored in a secure location
  2. APNS credentials are not included in version control
  3. Environment variables or secret management systems are used to store sensitive data

Requirements

  • Python 3.10+
  • httpx[http2] 0.20.0+
  • PyJWT 2.0.0+
  • cryptography 3.4.0+
  • Django 2.2+ (for Django integration)

License

MIT

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

simple_apns-0.1.2.tar.gz (22.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_apns-0.1.2-py3-none-any.whl (24.3 kB view details)

Uploaded Python 3

File details

Details for the file simple_apns-0.1.2.tar.gz.

File metadata

  • Download URL: simple_apns-0.1.2.tar.gz
  • Upload date:
  • Size: 22.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for simple_apns-0.1.2.tar.gz
Algorithm Hash digest
SHA256 41027fb05e4b94320e5eddd0d630172ab44153986ae382ed7b3b1d00517f09c6
MD5 c414c448baec931ed47deaa143099742
BLAKE2b-256 d6e6199eee531721b771df5c2df3e9aefc70b26c7ad540e614b0f89b89e04c0a

See more details on using hashes here.

File details

Details for the file simple_apns-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: simple_apns-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 24.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for simple_apns-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f66245299a2de5463f5f6c54aa1140896ce925c6ffe245eb67df2561106db357
MD5 b0f68d61139dd9348fdd4e4809770536
BLAKE2b-256 7af074a0805ec68759c8a41759ef116aaf74b87203bc042e1814d3f137be6f46

See more details on using hashes here.

Supported by

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