Adding iOS Push Notifications to Your Apps and Games

After a few days trying to figure out how to add push notifications to my newest game (coming in a week or two!), I finally figured out how the whole system works. I’m not going to do a full tutorial here, just elaborate on some existing tutorials I found and fill in some extra details that I had to figure out for myself.

Step 0: Some ISPs, including BlueHost, do not automatically open ports 2195 and 2196, which are used by APNS to handle push notifications. If you use one of these ISPs, you can contact customer support to open the ports. If it is BlueHost, you will likely have to pay extra (about $30/year currently) to have the privilege of opening them.

Step 1: use EasyAPNs. There’s an excellent video tutorial on their site to get you 95% of the way there yourself. Download the source, and follow the video to install Easy APNs on your web server.

Step 2: Make sure that any devices that are running a development provisioning profile version of your app are set to sandbox mode in your database, and that any devices running the ad-hoc version are set to production.

Step 3: You probably want to send messages to ALL users, rather than just one. To do that, you’ll just need an array including the pids of the devices you want from the devices stored in your apns_devices database. I ended up adding some code to my apns.php file to handle it. My code (which I mostly copied from a helpful poster at the Easy APNS Google code group) looks like this:

$sql = "SELECT `pid` FROM `apns_devices` WHERE `status` = 'active'"; 
$result = $db->query($sql);
$pids = array();
$i = 0;
while($row = $result->fetch_array(MYSQLI_ASSOC)){ 
 $pids[$i] = intval($row['pid']); 
 $i++;
}

When you create the new message, just make sure to pass in the $pids variable instead of a single number for the device you want to send to. This will create one message in the database for each device that is running your app.

Step 4: Make sure you tell the $apns object to run processQueue() at the end of your code.

Step 5: You need a way to easily add new messages, but make sure no repeat messages are sent to the same user. To handle this, I created a text file in the same directory as my APNS code. This is how I handled it in apns.php:

$message = file_get_contents("example.txt");
if($message != "")
{
 $apns->newMessage($pids);
 $apns->addMessageAlert($message); 
 $apns->addMessageSound('soft.caf'); 
 $apns->queueMessage();
}
file_put_contents("example.txt", "");

So the data gets read from the text file, provided there’s any text inside of it, and the contents of the file are cleared once the message is added. Boom. No repeat messages. And all you have to do to add a new one is add the message content to your text file.

Step 6: You want your cron job to run apns.php with the flush value passed in, which is explained back at Easy APNs. This will go through all the messages that have not yet been sent, and attempt to send them.

That’s it. Enjoy having your own custom-built push notification system for your apps.