Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome or Safari browser. Firefox 10 and Internet Explorer 10 should also handle it.

Sending Push Notifications With Urban Airship's Python Wrapper
by Matthew Makai
Presented to DC Python
Excella Consulting
April 3, 2012
Why?
Exponential mobile growth
Phones engage people in ways desktops cannot
New engagement type: push notifications

Push Notifications

iOS 4 iOS 5
Android
What are push notifications good for?

Games

Time-sensitive Discounts

Politics

After figuring out appropriate use cases for push notifications, how do we build the infrastructure to send them?

Urban Airship

What's an "Urban Airship"?
What is Urban Airship?
Apple Push Notification Service
Android C2DM
BlackBerry Push Service
Windows Mobile
Urban Airship's RESTful API
https://go.urbanairship.com/api/[action]
    
POST https://go.urbanairship.com/api/push/

GET https://go.urbanairship.com/api/ \
  device_tokens/[tokens]/

POST https://go.urbanairship.com/api/push/batch/
    
Register a Device
PUT https://go.urbanairship.com/api/ \
  device_tokens/FE66489F304DC75B8D6E8200DFF \
  8A456E8DAEACEC428B427E9518741C92C6660
    
Send a Push Notification
POST https://go.urbanairship.com/api/push/

{
  "device_tokens": [
    "ABC...",
    "FEG..."
  ],
  "schedule_for": [
    "2012-07-27 22:48:00",
    "2012-07-28 22:48:00"
  ],
  "aps": {
    "alert": "Hello from Urban Airship!"
  }
}
    

Python

Example push code
import urbanairship

app_key = 
  UA_KEYS[group_name]['UA_APPLICATION_KEY']

master_key = 
  UA_KEYS[group_name]['UA_MASTER_SECRET_KEY']

airship = urbanairship.Airship(app_key, 
  master_key)
    
android_json = {'android': {'alert': 
  'Android push notification text.'}}

airship.broadcast(android_json)

ios_json = {'aps': {'alert':
  'iOS push notification text.'}}

airship.broadcast(ios_json)
    
android_json2 = {'android': {'alert': 
  'Android push notification text.',
  'extra': {'url': 'http://example.com/content'}
  }}

airship.broadcast(android_json2)
    
urbanairship.py
BASE_URL = 'https://go.urbanairship/api'
BROADCAST_URL = BASE_URL + '/push/broadcast/'

def broadcast(self, payload, 
  exclude_tokens=None):

  if exclude_tokens:
    payload['exclude_tokens'] = exclude_tokens

  body = json.dumps(payload)
  status, response = self._request('POST',
    body, BROADCAST_URL, 'application/json')

  if not status == 200:
    raise AirshipFailure(status, response)
    

Where to go from here

Official Urban Airship Python API wrapper
Python wrapper fork w/ Android support
Geofencing w/ Urban Airship & CloudMine
Push Notification Tokens with Android C2DM
Apple Push Notifications Tutorial
Source Code to this Presentation
References