DEC 30, 2010
Simple APN Server
To never miss a gig, you definitely need to be notified of newly announced concerts. Shows 1.x had this, but it was flaky at best. And since I redesigned 2.0 from scratch, I took the opportunity to rewrite the whole APN server code as well.
PHP classes like php-apns and Ruby on Rails gems such as apn_on_rails and apnserver all will get you started for sending Apple Push Notifications (APNs). But they’re bloated as hell. APN_on_rails db migration, for example, creates 7 new tables in your database (for devices, groups, apps, notifications, etc.). That might be a fine setup for a huge webapp that processes a lot of synchronous notifications, but in my case, it seemed like overkill. And to be honest, I was curious to see how much code I could strip down. Here’s what I ended up with:
EDIT: RoR beast @pothibo just pointed out the APNS ror gem, which is pretty much what I was looking for from the start. But hey, at least now you know how it works!
PHP (for old time sake)
Again, the idea here was to send a push notification in the most minimalist fashion. No handling of errors, no talking with Apple’s feedback service, or device tokens validation. Nada. If you’re actually looking to make the whole thing work, you’ll need to keep a couple more things in mind:
- Development and distribution are two different environments. Apps signed for distribution (Ad Hoc or App Store), should use the distribution SSL certificate, private key (merged into a .pem file) and the non-sandbox gateway. Refer to Apple’s Push Notification Programming Guide to know how to create and install the SSL Certificates and Keys.
- The payload (the json part at the end of the APN message) can’t exceed 256 bytes. Make sure you’re handling that limitation.
- Apple doesn’t want you to open a socket for every notification you send. If you’re sending a lot of them within a short period of time, you’ll need to implement some kind of queuing system or just go with a full-fledged solution such as apn_on_rails.
- Beside sounds, badges and alert messages, the APN service permits an additional parameter that allows you to pass in additional arguments to your app (the ID of some object, for instance. See more examples).