App Development

How to Setup Push Notification with Urban Airship [3 Step Process]

Push notifications let your application notify a user of new messages or events even when the user is not actively using your application.

On Android devices, when a device receives a push notification, your application’s icon and a message appear in the status bar.

As per Google’s documentation “Google Cloud Messaging for Android (GCM) is a service that helps developers send data from servers to their Android applications on Android devices”.

Using this service, you can send data to your application whenever new data is available instead of making requests to the server in a timely fashion.

If you are developing cross platform applications, Phonegap is the best option to choose from and when it comes to push notifications, Urban airship is the choice for PhoneGap based apps.

Urban Airship supports push notifications for Android devices via Google Cloud Messaging service.  

Setup Push Notification Service Using Urban Airship

Let us see how to set up push notification service using Urban Airship.

Step 1 : Get your API Key from Google

In order to configure your Urban Airship application for GCM, you must first obtain an API key from Google.

The steps are:

1. Go to the Google Developers Console   https://console.developers.google.com/project

2. Create a project

3. Enable GCM by selecting   APIs & auth and turning the Google Cloud Messaging for Android toggle to ON.

4. Generate an API Key. Urban Airship takes care of API Access authorization for you, so you do not need to create an OAuth 2.0 client ID.

a) Select APIs & auth > Credentials.
b) Under Public API access, click “Create new key”.
c) In the Create a new key dialog, click “Server key”.
d) Do not specify any IP addresses in the form, and click “Create”.

5. Copy your key for server apps.

See this link  https://developer.android.com/google/gcm/gs.html  for more detailed instruction.

Step 2 : Configure Application in Urban Airship

Open up your application in your Urban Airship web application at go.urbanairship.com


1. If you do not yet have a login to the Urban Airship Dashboard, you will need to create an account. Account setup depends on your billing plan:

Enterprise Edition – contact  [email protected]  for access Developer Edition – go to go.urbanairship.com/accounts/register to begin the process.

Once your account information is entered, you will receive an activation email with an activation link.

Upon successful login you will see this initial default login screen after you log in:

2. The first time you log into the Urban Airship Dashboard, start out by configuring your first app. Click on the Create an App button

3. Create a New Application

4. Configuring Your Services. Navigate to the Settings > Services menu button in the left menu: In order to send a push, you need to configure your app to talk to Google’s Google Cloud Messaging (GCM).

Start out by clicking the Configuration button for the GCM service:

5. Enter your GCM API key and Applications package name, then  Save

Now you can see yourApplication on Your Urban Airship account’s home page.

6. App key and App Secret Select your app from the Dashboard Navigate to the Settings > API Keys menu button in the left menu: note down the generated keys for future use.

Step 3 : Setup Android Application for Push Service

In order to create an Android application, you need a basic knowledge about Android development environment. For more details check this link   https://docs.phonegap.com/en/edge/guide_platforms_android_index.md.html

1.  Create a Cordova Android application using Cordova Command Line. Use this link for more details    https://cordova.apache.org/docs/en/3.0.0/guide_cli_index.md.html


2.  Install the Phonegap – Urban Airship – push plugin using following command Cordova plugin add  https://github.com/urbanairship/phonegap-ua-push.git


3.  Find and copy js directory from  ~project-path/Example/js  and paste it to  www/js  in your application.
4.   Modify  www/config.xml  file to include the Urban Airship configurations.

<preference name="com.urbanairship.production_app_key" value="Your production app key" />
<preference name="com.urbanairship.production_app_secret" value="Your production appsecret" />
<preference name="com.urbanairship.development_app_key" value="Your development app key" />
<preference name="com.urbanairship.development_app_secret" value="Your development appsecret" />
<preference name="com.urbanairship.in_production" value="If the app is in production or not" />
<preference name="com.urbanairship.gcm_sender" value="Android only: Your GCM sender id" />
<preference name="com.urbanairship.enable_push_onlaunch" value="true" />

5.   In order to get AP ID, call  PushNotification.getPushId() method and use this AP ID to send Push.

6.   Add dependency to Server Application.

If Server Application is Maven Project, then add dependency to pom.xml

<dependency> <groupId>com.urbanairship</groupId> <artifactId>java-client</artifactId> <version>0.2</version> </dependency>

If Server Application is not Maven, then download library files from search.maven.org and add it to application’s lib folder.

7.   In Server Application create a package  com.urbanAirship , then Copy PushExample.java file from   https://github.com/urbanairship/java- library/blob/master/example/src/main/java/com/urbanairship/PushExample.java  paste it into  com.urbanAirship  and modify  sendPush()  method with parameters to send push.

For example:

public void sendPush(String deviceToken, String pushMessage){

String appKey = "appKey"; // add your appKey
String appSecret = "appSecret"; // add your appSecret
// Assign Device Token
String deviceToken = deviceToken;

logger.debug(String.format("Make sure key and secret are setKey:s Secret:%s",appKey, appSecret));

APIClient apiClient = APIClient.newBuilder()
.setKey(appKey)
.setSecret(appSecret)
.build();
logger.debug(String.format("Setup an APIClient to handle the API call %s", apiClient.toString()));
logger.debug("Send the message");

PushPayload payload = PushPayload.newBuilder()
.setAudience(Selectors.apids(deviceToken))
.setNotification(Notifications.notification(message))
.setDeviceTypes(DeviceTypeData.of(DeviceType.ANDROID))
.build();

try {
APIClientResponse<APIPushResponse> response = apiClient.push(payload);
logger.debug("PUSH SUCCEEDED");
logger.debug(String.format("RESPONSE:%s", response.toString()));
}
catch (APIRequestException ex){
logger.error(String.format("APIRequestException " + ex));
logger.error("EXCEPTION " + ex.toString());

APIError apiError = ex.getError().get();
logger.error("Error " + apiError.getError());
if (apiError.getDetails().isPresent()) {
APIErrorDetails apiErrorDetails = apiError.getDetails().get();
logger.error("Error details " + apiErrorDetails.getError());
}
}
catch (IOException e){
logger.error("IOException in API request " + e.getMessage());
}

}

Enjoy coding !!!

Are You Looking for Developers to Build Your Android/iOS App?

Perfomatix can help you fulfill your app development requirements, whether it be for Android, iOS, or Hybrid. 

Drop us a note to setup a meeting with experts in our app development team.

Perfomatix | Product Engineering Services Company