Using PHP to send notifications to Urban Airship

Previously I wrote about how to use Urban Airship for push notifications. I covered how to setup Urban Airship and your iPhone app code, however, you still need to write a server that sends the notifications to UA.

I decided to use PHP and develop locally on my Mac at first:

Get MAMP – I chose to get MAMP Pro too but you don’t need it.
Get the Urban Airship PHP lib (There are other libraries here).

You need to setup pear as the UA library depends on HTTP_Request:

/usr/lib/php/pear config-create $HOME .pearrc
/usr/lib/php/ install -o PEAR
/usr/lib/php//pear install install Net_URL
/usr/lib/php//pear install HTTP_Request

For MAMP you will need to add this to your script:

ini_set( 'include_path', ini_get( 'include_path' ) . PATH_SEPARATOR . "/usr/lib/php");

That’s it! Put your app key/master secret in the sample code and test.

Once it’s looking good you could continue to run on your Mac, but I decided to put on my web hosting server.

I’m using DreamHost, so with help from here, perform the same pear installs:

/usr/local/php5/bin/pear config-create $HOME .pearrc
/usr/local/php5/bin/pear install -o PEAR
/usr/local/php5/bin/pear install install Net_URL
/usr/local/php5/bin/pear install HTTP_Request

Then to your script, add:

ini_set( 'include_path', ini_get( 'include_path' ) . PATH_SEPARATOR . "/home/.[machinename]/[username]/pear/php");

Later you might want to run from the command line via cron – make sure you use PHP5:

/usr/local/php5/bin/php -f myPush Server.php >> log.log 

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.