Using Amazon SNS Mobile Push for iOS

Aug 23, 2013 · 3 min read · technical

Amazon recently launched mobile push from Amazon SNS. SNS (Simple Notification Service) acts as a middle-man between your product and the relay service (Apple Push Notifications, Google Cloud Messaging etc) which means that you don’t need to worry about establishing connections, queuing messages, handling feedback, or scaling. It comes with a million push notifications a month to start with - which should be enough for now.

Before you start

You need an iOS Developer account so you create provisioning profiles to build to your device (and to get push working)

Signup for Amazon Web Services.

Xcode & OSX.

If you don’t use iOS, AWS has a good post about how to get started with Android on their blog

So what do I do?

We’re going to be creating an app and a provisioning profile for it. For convience, you can just download the example app from the documentation (Navigate to the iOS Project) If you’ve worked with push notifications before, you’re probably familiar with the process and can refer to the documentation. Open up the sample project and change the app identifier to something like this (It’ll be in the app’s info.plist)

com.yourname.app

Follow the detailed instructions here from the documentation (Navigate to the iOS Project) up to step 23 here to create a .p12 file Once you have the p12 file, run this command in Terminal (in the right directory) (replace the file names)

openssl pkcs12 -in myapnsappprivatekey.p12 -out myapnsappprivatekey.pem -nodes -clcerts

Now just sign the example app with the right developer certificate and provisioning file (the one you just generated that has push notifications enabled) and when you build and run, you’ll get a Device Token kind of like this

Device Token = <examp1e 29z6j5c4 df46f809 505189c4 c83fjcgf 7f6257e9 8542d2jt 3395kj73>

Copy the token and navigate to the SNS management page on the EC2 Dashboard. If you get an error that says something like aps-enviroment not found, the application isn’t being signed with the right provisioning profile. Check the app identifier and the build settings.

Click on Add a New App and select the APNS Sandbox platform since we are provisioning the app with a development profile. Upload the .pem file that was generated from the command line and when you press load credentials from file, they should load in the boxes below.

You’ll now see a window at the bottom with Endpoint Actions and Add Endpoints. Each endpoint represents a device. Just add your device by pressing Add Endpoints and manually copy in your device token. You can leave the user data field blank.

Once you’ve added the device, you can publish to it manually type in something like, “Hello World!” and press publish.

If you want to get fancier try adding sounds and badges to your alerts, you can do so by changing the actual payload of the message. Try something like this

{
 "APNS_SANDBOX":"{\"aps\":{\"alert\":\"Hello World!\",\"badge\":5,\"sound\":\"default\"}}"
}

Other things to note.

The mobile client can’t register with SNS directly so you’ll still have to maintain a proxy server that will handle registrations.

Use of SNS client libraries is required to send push notifications programatically - but the advantage is that it’s going to be cross platform.

If you’re having trouble using SNS to send the notifications try using a small utility to send notifications from your computer