PhoneGap 1.8 with Urban Airship
I've been doing some iOS coding using phonegap and JQuery Mobile. It's a great combination and means I can develop for iOS without having to touch ObjectiveC, a language frankly made of evil.
Wanting to be able to do push notifications, a quick google pointed me to this post on using urban airship. However, the articles incomplete, and it's for a very old version of phonegap. About 8 million googles, some pathetic attempts at attempting to understand objective C, and I got it all working together nicely. I thought I'd blog about the experience of how to get it up and running, so anyone else banging their head against a brick wall can save themselves 4 hours of pain.
1) Download SACordova
This is the basic template for push notifications and should be enough to get you going. It's fine if you're wanting to introduce push to an existing app, you'll still need the code from this app. The original code was by PhoneGap/Airship, and is in the link above. This was then adapted for 1.5 by HMKang . All I've done is update it for 1.8. All the instructions are still valid;
- Overwrite your AppDelegate.h and AppDelegate.m with the ones from the project
- Add PushNotification.m and PushNotification.h to you plugins folder
- In Cordova.plist, add pushnotification/PushNotification to the plugins list:
- Paste the index.html and PushNotification.js into your www folder. You can pick and choose which bits of index you need, but I recommend just using the whole thing to start (I burnt a fair amount of time trying to be clever. I really should stop doing that).
You should now be set up except for your keys.
Apple & Urban Airship
Truely it is not an understatement to say that apple don't make it easy. You need a push certificate to be able to do push notifications. You can follow most of the instructions on the original phonegap tutorial for this, but just wanted to put in some gotchas.
- Generate a new CSR!! If you've got previous apps, it's tempting to use that cert but you can't. You must generate a new one from your Keychain Access.
- When I first attempted to export to a p12 it wasn't available as an option. I exported as a PEM, but then airship complained the private key wasn't available. A Quick google indicates that the cert was somehow corrupted. Basically, just go back, revoke your cert and re-generate it. In your keychain, you should be able to see a key underneath it, like below:
The private key name should match whatever you put in your CSR request. You should now be able to generate your p12 file.
Over to the airship
Go, sign up etc. When creating a new app, put your p12 in and your password. All should be well in the world.
Now the important bit. Plonk your application Key and Secret at the top of AppDelegate.m (keep the URL the same)
Pulling it all together
One last thing! You need to add '*.urbanairship.com' to your externals hosts. Else it can't contact the server.
Run the app on a device (emulators don't work!), and hit the button. You should get a nice pop up asking for permission to do push notifications. If it doesn't work, check the logs in xcode. I hit:
didFailToRegisterForRemoteNotificationsWithError:Error Domain=NSCocoaErrorDomain Code=3000 "no valid 'aps-environment' entitlement string found for application"
This is another one you'll hit if you've done an app before. You need to ensure you have a provisioning profile set up for your new application. Head over to the apple dev site again, and create and download a new provisioning profile. Drag and drop it into xcode. Now run it. Victory! (I hope).
When creating this, I did not have victory. I've been bashing my head trying to fix it. I've done all the fixes, which basically mean deleting provisioning profiles, regenerating them etc.
To save yourself an hour, ENSURE YOUR PROVISIONING PROFILE matches your full app ID, e.g.
XXXXX.com.yourname.app.
I'd just done
XXXX.app.
IF you do this, regenerate your App, do all the cert stuff again, reupload to urban airship, and all will be well!








