Push Notification using Urban Airship

January 31, 2014

Recently, we received a requirement from a customer to implement push notification in their app. They already had an account set up with UrbanAirship so they wanted us to continue to utilize their push notification services. UrbanAirship’s push notification service is a service that defines a single interface for push notifications to all types of phones (Android, iPhone, Blackberry and Windows Phone). This is a great service because it uses UrbanAirship’s database to store the device type associated with the user. There is then less required for the team to store on their side and less code to write. The only difficulty lies in setting up the service; connecting it from back end, all the way out to the end user.

The application we set up with UrbanAirship has an iPhone and Android component, so most of this tutorial will describe specifically these two . There are three separate components to set up in order to connect the back end to the end user (through UrbanAirship). The back end connects to UrbanAirship and UrbanAirship sends the push notifications to the end user (typically through a platform specific service like Apple Push Notification Service or Google Cloud Messaging).

The Back End

The only thing that needs to be done one the back end is to invoke the UrbanAirship API to send a push notification. The timing of this is up to your implementation. You can trigger events to happen at certain times, or based off certain events. Our implementation triggers off an event happening. Once the trigger happens, sending a push notification is as easy as sending a POST (more details here). The easiest way to signify the user is to implement an alias for each user. UrbanAirship will then tie the alias to the device identifier. For example, if the user has to sign-in to the application, using their username (or a hashed equivalent of their username) as the alias makes sending push notifications easier. If you specify an alias in your POST, UrbanAirship will send the push notification to every device associated with that alias.

Urban Airship

UrbanAirship must be configured to send out the push notifications to the applications. The first step is to set up the applications in UrbanAirship. After logging in, select “New App” from the main page. Give the application an arbitrary name and select “debug” or “production” depending on your current application status.

After the application has been created, you have to set it up with the services you want to use (APNS, GCM, etc). To do this, select the application’s settings and select “Services”, then select “Configure” the services you need to employ. For Apple Push Notification Services (APNS), follow these instructions. For Android’s Google Cloud Messaging (GCM) Service, follow these instructions.

Under the application’s settings, you will find the “API Keys”. These keys will be used later in your application.

End User

Android

The first step to getting UrbanAirship connected to the Android application is to add the UrbanAirship library to the project and ensure it is bundled in your final application. The application then needs the correct permissions assigned in the AndroidManifest.xml in order to accept the push notifications. An example AndroidManifest.xml file can be found here.

After all the prep work is complete, the application needs to know which UrbanAirship application to point to. This is where the application keys provided for you from UrbanAirship come into play. There are two ways to use the application keys. You can create an airshipconfig.properties file (in the assets folder) which contains your application keys, or you can set the application keys in code with the following code block (in your Application’s OnCreate method):

AirshipConfigOptions options = AirshipConfigOptions.loadDefaultOptions(this);
options.developmentAppKey = 'Your development app key';
options.productionAppKey = 'Your production app key';
options.inProduction = false; //determines which app key to use

The inProduction flag tell the application to use the production application key, or the development application key.

After the options are set up, all that is left is to enable push notifications. This can be done by adding the proper API calls to the Application’s OnCreate method.

UAirship.takeOff(this, options);
PushManager.enablePush();

Now your application is set up to accept push notifications. If you have decided to use aliases (recommended), you need to register your alias with UrbanAirship. This can be done anywhere in the application with the following code:

PushManager.shared().setAlias('AliasString');

Now, what to do when a push notification actually comes in? All you have to do is register for the ACTION_NOTIFICATION_OPENED intent.

iOS

Note: Assuming the application has been set up for push notifications in the Apple developer portal.

The iOS version of our application is built using Xamarin Studio instead of Xcode. If you are looking for instructions for Xcode, you can find them here. Xamarin setup is a little bit different. There are two ways to setup the push notifications. The first is using a pre-built Xamarin binding for UrbanAirship. There are plenty of samples included with that project to understand how to use it. The other way (and the way we used) is to do a PUT to UrbanAirship’s API ensuring your device is registered. This PUT uses the UDID of the device to register with UrbanAirship. You can also set an alias in this PUT by adding the alias to the JSON passed to the API. Here is some code used to register the user with the application on UrbanAirship:

string putUrl = 'https://go.urbanairship.com/api/device_tokens/' + udid;
var request = (HttpWebRequest)HttpWebRequest.Create (putUrl);
request.Method = 'PUT';
request.ContentType = 'application/json';
/* Send along the application key and application secret */
request.Credentials = new NetworkCredential (urbanAirshipApplicationKey, urbanAirshipApplicationSecret);
/* set up an alias */
string payload = '{alias: ' + alias + '}';
var byteArray = Encoding.UTF8.GetBytes (payload);
request.ContentLength = byteArray.Length;
var dataStream = request.GetRequestStream ();
dataStream.Write (byteArray, 0, byteArray.Length);
dataStream.Close ();

var response = (HttpWebResponse)request.GetResponse ();

Testing

After setting up the system, it is best to send some test push notifications. The easiest way to do this is using a tool that can POST data (like curl or a browser extension like the REST Console). A sample curl call is shown below which includes the following variables:

  • : UrbanAirship Application Key
  • : UrbanAirship Application Master Secret
  • : the message to send to the Android phone for the specified user (if applicable)
  • : the message to send to the iOS device for the specified user (if applicable)
  • : the alias of the user to send it to (as registered with UrbanAirship).
curl -X POST -u ':' 
-H 'Content-Type: application/json'
--data '{"android":{"alert": ""}, "aps": {"alert": ""}, "aliases":[""]}'
https://go.urbanairship.com/api/push/

UrbanAirship

There are many other things UrbanAirship can do like Rich Push Notifications, Mobile Analytics, Wallet Services and Audience Segmentation. Check them out!

Looking for more like this?

Sign up for our monthly newsletter to receive helpful articles, case studies, and stories from our team.

How to Bring Order to the Chaos of Job Applications
Team

How to Bring Order to the Chaos of Job Applications

August 8, 2023

We discuss some tips & tricks for the job application process

Read more
3 tips for navigating tech anxiety as an executive
Business

3 tips for navigating tech anxiety as an executive

March 13, 2024

C-suite leaders feel the pressure to increase the tempo of their digital transformations, but feel anxiety from cybersecurity, artificial intelligence, and challenging economic, global, and political conditions. Discover how to work through this.

Read more
Kotlin Multiplatform
Android Development iOS

Kotlin Multiplatform

July 14, 2022

A brief look at Kotlin Multiplatform Mobile, a newer cross-platform mobile application framework.

Read more
View more articles