Friday Lab for 2013-12-20 (part 2)

Sunday, 2013-12-22


As I mentioned the other day, today I'm going to try to wrap up my PHP + MySQL push notification web service. I'll also allow myself a bit more time and do a production-quality job on module organization and abstraction. I'll be abstracting data access and the actual push notification mechanism.


The insertions and querying of apps table are now working. All the database code has been abstracted into a separate Database class - nothing sophisticated, just a bunch of static functions being invoked from the main module. But it will allow easy replacement if needed be.

Now I'm wondering how do I do unit tests in PHP. I'll definitelly add a black box test which will delete all the data (not so black box but it's a setup) and then test the entire API.


After some minor detours, the logins are working and so is token validation. Next on is the message sending - at least the API part.

I find PHP documentation (as found on to be truly great: its complete, clean, easily digestible and community feedback is much ritcher than say on MSDN.


I've added states for logins and messages, fatal error handling, bunch of other stuff. The one thing that isn't working yet is payload parsing for send API method. Or I'm sending badly formed JSON data...


I finally tracked down the problem to not requesting json_decode to return the result in an associative array. But it's Sunday so it's kids' time as well and we are now off to MIM. I'll wrap this up in PM.


Though shall not make work plans on Sunday! But back in the saddle...


Everything should be working now but now I need to test it against APNS and for that I need test certificate. I'm using ApnsPHP library and this time I forked it before I included it as a Git submodule. So any pushes I might have against it will go to my Git repository and not the original.

Anyway, I got my certificate on the local drive together with my private key and I'm now going to convert them to PEM files used by ApnsPHP. I usually follow the instructions described in this article ("Making the App ID and SSL Certificate" section).


After some testing and failing I got push working but of course there is no app that is supposed to be receiving these notifications. I have to create App ID when I was applying for push certificate and that's the one that I'll use now to create an iPhone app.


After the domestic peace has returned (kids are asleep) I'm wrapping this up:

  1. New iOS app.
  2. Add push notification code code to app's delegate class*.
  3. Run the app (but not in the simulator!)
  4. Grab the device token from the debugger output and clean it up for sending to API.
  5. Close the app on your device.
  6. Issue a new API call to send a message.
  7. Receive the message!

It's a wrap!

* To adapt app's delegate, in didFinishLaunchingWithOptions add:

//  Every time we launch we have to register the device for remote notifications.
[application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];

Also add the following code to the application delegate:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"Device token is %@", deviceToken);

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"Push notification registration failed: %@", error);


Let's see about some statistics. RescueTime reports that I've been on my Mac for 6 hours and 7 minutes today out of which 6 hours and 2 minutes (!) were spent productively. Breaking it down, I spent 02:45 in Sublime Text 2, 01:41 in Terminal and the 3rd biggest application I used was... Stack Overflow where I spent 11 minutes.

So to sum it all up I've spent the total of about 6 hours coding and the rest (about 11 hours) monkeying around with the infrastructure and running API tests (which I always ran with curl from Terminal).

Ah, there is one more thing that I need to do...


I added the last thing - updating the message status to Sent together with some fixes. I'll document the API tomorrow (it's well commented but that's not the same thing) and publish the source code on GitHub.

Speaking of Git, I have the total of 131 commit out of which 29 were due to typos. I wish I had that kind of statistics for all my development (reminds me of data collected for PSP - I should look into that again).


Ivan Erceg

Software shipper, successful technical co-founder, $1M Salesforce Hackathon 2014 winner