django-iphone-push

Add iPhone Push notifications to a Django Project quickly and easily

Introduction

This is a very simple Django project that will allow you to add the ability to push notifications to your iPhone Application quickly and easily.

As you can see from the shot from the left. I used this code to implement push notifications for my upcoming iPhone Application called Tube Notify. I'm sure release this will now mean someone might copycat me, but I wanted to share this code so that others could get through the mystification of Apple's APN service.

I'll add to this page with more examples at some point in the not-to-distant future.

License

You are free to use this code for whatever purpose you like in FREE iPhone applications. However, if you want to charge for your application, you must ask permission first. Sorry.

Disclaimer

Naturally, I am not responsible for this. If you screw up and make it send notifications to lots of phones and end up upsetting Apple, that's YOUR fault not mine.

Installation

This application at least requires Python 2.5. It does make use of some Python 2.6 features. If you want to use on Python 2.5 you'll need the SSL Socket backport.

See the github repo for django-iphone-push for details of clone URLs etc.

To add this to an existing project, use the git submodule command within your django project and then add to the INSTALLED_APPS.

I use south for migrations. You can use them or just syncdb.

You then need to ensure you sort out your push certificates. You'll need a sandbox one and a live one. To do this there are a variety of guides but the best one is probably the one at MacCoders. For this application you need to ensure they are in .pem format.

You then need to set up your settings.py to look something like this:

# Full path to the APN Certificate / Private Key .pem
IPHONE_SANDBOX_APN_PUSH_CERT = os.path.join(PROJECT_ROOT, "iphone_ck.pem")
IPHONE_LIVE_APN_PUSH_CERT = os.path.join(PROJECT_ROOT, "iphone_live.pem")

# Set this to the hostname for the outgoing push server
IPHONE_SANDBOX_APN_HOST = 'gateway.sandbox.push.apple.com'
IPHONE_LIVE_APN_HOST = 'gateway.push.apple.com'

# Set this to the hostname for the feedback server
IPHONE_SANDBOX_FEEDBACK_HOST = 'feedback.sandbox.push.apple.com'
IPHONE_LIVE_FEEDBACK_HOST = 'feedback.push.apple.com'

Note the full path to the .pem files. I use a shortcut of getting the PROJECT_ROOT at the top of my settings.py for various things. For reference:

import os
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))

With all that done, you're ready to use the app!

Usage

I won't go into how to set up your django project to make use of this. However, in terms of sending messages you would be good to read Apple's documentation on the topic. It covers the detail of the send_message function in the iPhone.

There is a way to send to a group of phones. This is advisable when dealing with lots of notifications around the same time. Simply because Apple says so in their documentation.