Urban Airship Python Library
The `urbanairship` library is a Python client for interacting with the Airship REST API, enabling programmatic access to features like sending push notifications, managing audiences, and retrieving reports. It is currently at version 7.3.1 and receives updates periodically, with major releases introducing significant changes.
Common errors
-
AttributeError: module 'urbanairship' has no attribute 'Airship'
cause Attempting to use the `Airship` client class in `urbanairship` versions 7.0.0 or newer. This class was removed.fixReplace `urbanairship.Airship(key, secret)` with `urbanairship.BasicAuthClient(key, secret)` or `urbanairship.BearerTokenAuthClient(token)`. -
TypeError: 'int' object is not subscriptable
cause In `urbanairship` versions before 7.3.1, sending a `message` without a `notification` could improperly raise a `TypeError`.fixUpgrade to `urbanairship` version 7.3.1 or newer. If upgrading is not possible, ensure that when sending a `message`, a `notification` object is also included or structure your payload appropriately to avoid this edge case. -
ImportError: cannot import name 'Airship' from 'urbanairship'
cause Attempting to import the `Airship` client class directly from the `urbanairship` package in versions 7.0.0 or newer. This class no longer exists.fixChange your import statement from `from urbanairship import Airship` to `from urbanairship import BasicAuthClient` or `from urbanairship import BearerTokenAuthClient`.
Warnings
- breaking The primary `urbanairship.Airship` client class was removed in version 7.0.0. Code using this class will raise an `AttributeError` or `ImportError` on v7.x.x.
- breaking Version 7.0.0 dropped support for Python versions prior to 3.10. Older versions (e.g., v6.0.1) dropped support for Python 2 and prior to 3.6.
- gotcha The official GitHub README for the `urbanairship/python-library` project may contain outdated quickstart examples that still reference the removed `urbanairship.Airship` class, even for current 7.x.x versions. Following these examples will lead to errors.
- breaking Support for location APIs, including the `LocationFinder` class and location-based audience selectors, was removed in version 6.0.1.
Install
-
pip install urbanairship
Imports
- BasicAuthClient
from urbanairship import BasicAuthClient
- BearerTokenAuthClient
from urbanairship import BearerTokenAuthClient
- Airship
from urbanairship import Airship
N/A (removed in v7.0.0)
- Push
from urbanairship import Push
- Audience
from urbanairship import Audience
- Notification
from urbanairship import Notification
- Platform
from urbanairship import Platform
Quickstart
import os
from urbanairship import BasicAuthClient, Push, Audience, Notification, Platform
# Replace with your actual credentials or set as environment variables
APP_KEY = os.environ.get('URBAN_AIRSHIP_APP_KEY', 'YOUR_APP_KEY')
MASTER_SECRET = os.environ.get('URBAN_AIRSHIP_MASTER_SECRET', 'YOUR_MASTER_SECRET')
if APP_KEY == 'YOUR_APP_KEY' or MASTER_SECRET == 'YOUR_MASTER_SECRET':
print("Please set URBAN_AIRSHIP_APP_KEY and URBAN_AIRSHIP_MASTER_SECRET environment variables or replace placeholders.")
# exit(1) # Uncomment to prevent execution with dummy credentials
# Initialize the client with Basic Authentication
client = BasicAuthClient(APP_KEY, MASTER_SECRET)
try:
# Create a Push object
push = Push(client)
# Target all devices
push.audience = Audience.all()
# Define the notification message
push.notification = Notification(alert="Hello from Urban Airship Python Library!")
# Specify device types (platforms) to target
push.device_types = Platform.all()
# Send the push notification
response = push.send()
print(f"Push notification sent successfully! Status: {response.status_code}")
print(f"Response body: {response.json()}")
except Exception as e:
print(f"An error occurred: {e}")