APNs2 Python Library

0.7.2 · active · verified Tue Apr 14

apns2 is a Python library for interacting with the Apple Push Notification Service (APNs) using the HTTP/2 protocol. It supports both certificate-based and token-based authentication. The current version is 0.7.2, with development showing sporadic updates, often addressing dependency compatibility and APNs specification changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates sending a push notification using token-based authentication, which is recommended by Apple. Ensure you have your APNs authentication key (.p8 file), Key ID, Team ID, app bundle ID, and a target device token. These values are typically obtained from your Apple Developer account. It uses environment variables for sensitive credentials.

import os
from apns2.client import APNsClient
from apns2.payload import Payload
from apns2.credentials import TokenCredentials

# Environment variables for token-based authentication
# Set these in your environment or replace with actual values for testing
AUTH_KEY_PATH = os.environ.get('APNS_AUTH_KEY_PATH', 'path/to/AuthKey_ABCDEFG.p8')
AUTH_KEY_ID = os.environ.get('APNS_AUTH_KEY_ID', 'ABCDEFG')
TEAM_ID = os.environ.get('APNS_TEAM_ID', 'XXXXXXXXXX')
DEVICE_TOKEN = os.environ.get('APNS_DEVICE_TOKEN', 'your_device_token_here')
APP_BUNDLE_ID = os.environ.get('APNS_APP_BUNDLE_ID', 'com.example.YourApp')

if AUTH_KEY_PATH == 'path/to/AuthKey_ABCDEFG.p8' or \
   AUTH_KEY_ID == 'ABCDEFG' or \
   TEAM_ID == 'XXXXXXXXXX' or \
   DEVICE_TOKEN == 'your_device_token_here' or \
   APP_BUNDLE_ID == 'com.example.YourApp':
    print("WARNING: Please configure APNS_AUTH_KEY_PATH, APNS_AUTH_KEY_ID, APNS_TEAM_ID, APNS_DEVICE_TOKEN, and APNS_APP_BUNDLE_ID environment variables or replace placeholders in the script.")
    # Exit or use dummy values for demonstration if actual push is not intended
    # For a runnable example without valid credentials, one might comment out the actual send_notification call
    # For this registry, we will print a warning and allow it to run potentially failing.

try:
    # Initialize TokenCredentials
    credentials = TokenCredentials(
        auth_key_path=AUTH_KEY_PATH,
        auth_key_id=AUTH_KEY_ID,
        team_id=TEAM_ID
    )

    # Initialize APNsClient with token credentials
    # For development (sandbox) environment, use_sandbox=True
    client = APNsClient(credentials=credentials, use_sandbox=True) # Set to False for production

    # Create a push notification payload
    payload = Payload(alert="Hello from apns2!", sound="default", badge=1)

    # Send the notification
    response = client.send_notification(DEVICE_TOKEN, payload, APP_BUNDLE_ID)

    print(f"APNs Response Status: {response.status}")
    print(f"APNs Response Reason: {response.reason}")
    if response.apns_id: # Optional, depending on library version/response structure
        print(f"APNs ID: {response.apns_id}")

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

view raw JSON →