Report abuse

#
# Sample code for using the GoogleAppEngine (Python) with UrbanAirship to send
# out iPhone (OS3.0) Push Notifications.
#
# @killingmichael 
# http://twitter.com/killingmichael
#
# Based on code by:  http://twitter.com/bryanbartow
#  
#
import logging
from django.http import HttpRequest
import urllib
import base64
from google.appengine.api import urlfetch
from django.utils import simplejson


def sendApplePushNotification(name):
    # via UrbanAirShip
    logging.info("sending notification... "+name)

    UA_API_APPLICATION_KEY = 'APPLICATION KEY GOES HERE'  #Application Key from UrbanAirship -> App Menu -> App Details to Display
    UA_API_APPLICATION_PUSH_SECRET = 'APPLICATION PUSH SECRET GOES HERE'  #Application Secret from UrbanAirship -> App Menu -> App Details to Dispaly.
    url = 'https://go.urbanairship.com/api/push/'

    auth_string = 'Basic ' + base64.encodestring('%s:%s' % (UA_API_APPLICATION_KEY,UA_API_APPLICATION_PUSH_SECRET))[:-1]

    alertMsg = "Hello from " + name
    badgeNumber = 3
    deviceToken = "DEVICE TOKEN GOES HERE"

    body = simplejson.dumps(   {"aps": {"badge": badgeNumber, "alert": alertMsg}, "device_tokens": [deviceToken]}  )

    data = urlfetch.fetch(url, headers={'content-type': 'application/json','authorization' : auth_string}, payload=body,method=urlfetch.POST)

    if data.status_code == 200:
        logging.info("Remote Notification successfully sent to UrbanAirship "+str(data.status_code))
    elif data.status_code == 400:
        logging.error("Remote Notification not sent! Do something smart now or not :) "+str(data.status_code))