A great way to keep mobile app users engaged over time is to send relevant push notifications on a regular basis. If you want to keep your app away from app folder obscurity, push notifications can breath new life into the interactions with users. However, as with most prescriptions, use moderation for best results. Seek to set a great balance with your users to prevent being removed entirely.
There are several platforms and services that enable push, but one of the simplest and perhaps most overlooked is Amazons Web Services (AWS). One of the very first services launched on AWS was the Simple Notification Service (SNS). And not too far behind, support for mobile app push notifications via SNS was launched. Amazon does a great job of simplifying the plumbing for sending push notifications and manages the interface between your application and the platform specific push implementations.
AWS SNS is also really cheap for sending push notifications. The pricing is simple and as little as $1.00 to send a million push notifications per month.
Let's dive into a step by step guide to setup push notifications with AWS.
In order to test push notifications from AWS, let's start by creating a simple iOS application. AWS supports push notifications for iOS, Android, Amazon, Windows, and more. We'll focus on iOS here for simplicity, but you'll find it's a very similar process to add push in an Android app and others.
It's worth noting that Amazon has a new AWS Mobile Hub that brings together several mobile related services and APIs to make mobile integration with AWS easier. If you've used Parse in the past, it has a similar feel to it. However, for this article, I will not be using AWS Mobile Hub. The steps here would be more similar to the process you would go through to add push notifications to an existing iOS application.
Push notifications require a unique App ID for your app.
Alright, now that we have our App ID, we're ready to create the iOS project. Keep the Apple Developer web interface around, we'll be headed back there shortly.
Feel free to clone that repository and use it for reference or just use it for your sample app. If you use the sample app, remember to go in and change the bundle ID as described below to use your bundle ID and ensure that the push notifications capability is enabled.
Setup your App ID in the Xcode Project:
Tap over to the "Capabilities" tab and enable the push notifications for your app by turning the switch on.
In the sample app, I've implemented most of the logic for handling push notifications in a class called PushManager. The class does three important things:
Below is the PushManager class. You'll see calls to request authorization and if permission is granted, register for remote notification types. Once we successfully register for remote notifications with Apple, a device token is provided and stored at the class level.
When a push notification is received on the device, iOS will present the notification as long as the app is not foregrounded. However, if the app is in the foreground, iOS will by default not present the notification. It is up to the app to change the UI accordingly or present the notification in the app. With iOS 10 and above, there is now the ability to tell iOS to still present the system UI for the notification even if the app is presented.
You'll noticed in the UNUserNotificationCenterDelegate handler, we call:
This will cause iOS to still present the notification at the system level.
We need to wire up the PushManager in the AppDelegate when the app launches.
The UNUserNotificationCenter class has a delegate that can be assigned in order to process notifications as they are received. Inside the AppDelegate, we register PushManager to be the delegate inside didFinishLaunchingWithOptions:
You'll also notice that the AppDelegate above has methods to pass along the response to:
PushManager will handle the token and results so we simply pass it along.
Build and run the Push app on an iOS device. It is worth noting that push notifications only work on iOS devices, so you won't be able to test with the Simulator. If you run on a device, you should be prompted to allow push notifications. If you accept push, after the registration with Apple completes, the device token will be shown on the main view controller of the app. The device token will also be logged to the console. You'll need that token to send your first push, so make a copy from the console log.
Ok. We've got our iOS app setup and ready to handle push notifications. Now let's setup AWS.
First, we need to create and export a certificate key pair for AWS to use to connect to the Apple Push Notification service. In the Apple Developer portal, navigate to the Certificates, Identifiers and Profiles section again. Find the App ID that you created for the sample app in the App ID list and click on it to expand the details. Click the edit button at the bottom to edit the App ID. Scroll to the bottom of the list of capabilities to the Push Notifications area.
There are two configuration options, Development and Production. We'll use the development environment for testing so we'll just configure that for now. The Production configuration is for push notifications sent in Apple's production environment to App Store downloaded apps.
Click on "Create Certificate" to create a new certificate/key pair to use for development push. You'll be walked through the process of generating a Certificate Signing Request in Keychain and then generating and downloading the certificate. Once you've installed the certificate in Keychain, you should export it to a .p12 file. Option click on the certificate/key pair that you generated and you'll see an option to export. Provide a password for the .p12 file and be sure to record it.
Now we're ready to log in to AWS and setup and test push notifications. Log in to your AWS account at console.aws.amazon.com. If you don't already have an AWS account, you can sign up for a Free Tier account that includes lots of free tier service to try out for a year.
Inside the AWS console, navigate to:
Services -> Application Integration -> Simple Notification Service
From the SNS Dashboard, click on "Create platform application". A pop up to configure your app appears. Enter an application name and select "Apple Development" from the "Push notification platform" drop down. You'll notice that there are other options in the platform drop down for "Apple production", "Google Cloud Messaging", etc. that allow you to configure other push notifications types as needed. Let's stay focused on Apple Development for now.
You'll notice the screen will now show an area for you to configure your iOS push certificate and upload the .p12 file you exported from Keychain for your push certificate/key pair.
Choose your .p12 file and enter the password you set for it and then press "Load credentials from file" and you'll see the bottom boxes populated with your certificate key pair. Click "Create platform application" to complete the setup.
You should now see the application you just created under the "Applications" area of SNS. The listing should show an ARN that you can click on. If you click on the ARN, it will load the list of application endpoints configured for your application. You can think of an endpoint as a device token in Apple's push network. It uniquely identifies a single application install on a single device.
Let's add an endpoint for your push device token. Click "Create Platform Endpoint" and a screen will pop up to prompt for the device token and user data.
Enter the device token that you copied from the iOS app log console earlier or copy it from the main screen of the app. In user data, you can enter a friendly description that will help you remember what that endpoint is.
Ok, now that you have your endpoint confgured, there should be a checkbox all the way on the left of the list of endpoints. Check that box and the blue "Publish to Endpoint" button above the list should enable. Click that to pop up the message compose window.
You'll notice that you're given the option to create a message using raw formatting or JSON. The content of a push notification has a specific format to specify things like the message, sound, custom details, etc. We're going to keep it simple and just send an alert message. AWS even has a simple JSON message generator that will allow you to simply type in your message and it will generate the formatted JSON needed for you. Tap on the "JSON message generator" button and fill in the pop up with your first push message. Be creative, this is going to be popping up on your device shortly.
Once you've got your message, you can leave everything else as default and click the blue "Publish Message" button at the bottom. Boom! Your message should be on its way from AWS over to Apple and then popping up on your device.
Hopefully you've had some fun sending test push notifications to your device from the AWS Console. Now you're ready to integrate push into your broader application. Perhaps you have a back end system that you plan to send push notifications from. Integrate with all that AWS has to offer in order to make it super simple to send push. Use API Gateway to expose a simple API for your back end to consume and send push to remote clients. Or use AWS Lambda functions to initiate push as a response to some other event in AWS.
Looking for some help integrating with AWS? Centare has a talented team of AWS certified professionals to help.