Author: Daniel Passos (dpassos)
Level: Intermediate
Technologies: Java, Android
Summary: The push-contacts-mobile-android
quickstart demonstrates how to develop a contacts CRUD mobile Android application with push notification integration.
Prerequisites: push-contacts-mobile-picketlink-secured
Target Product: JBoss Unified Push
Versions: 1.0
Source: https://github.com/jboss-developer/jboss-mobile-quickstarts/
The push-contacts-mobile-android
quickstart demonstrates how to develop more advanced Android push applications, centered around a CRUD contacts application.
This client-side Android project must be used in conjunction with the push-contacts-mobile/server/push-contacts-mobile-picketlink-secured
application, which provide the accompanying server-side functionality.
When the client application is deployed to an Android device, the push functionality enables the device to register with the running JBoss Unified Push Server OpenShift instance and receive push notifications. The server-side application provides login authentication for the client application and sends push notification requests to the JBoss Unified Push Server in response to new contacts being created. Push notifications received by the Android device contain details of newly added contacts.
- Java 7
- Maven 3.1.1
- Latest Android SDK
- Latest Platform Version and Platform version 19
- Latest Android Support Library and Google Play Services
This quickstart is designed to be built with Maven. It requires the JBoss Unified Push Maven repository and Google libraries.
You must have the JBoss Unified Push Maven repository available and Maven configured to use it. For more information, see the Configure Maven or the README distributed with the JBoss Unified Push Maven repository.
Google does not ship all the required libraries to Maven Central so you must deploy them locally with the helper utility maven-android-sdk-deployer as detailed here.
-
Checkout maven-android-sdk-deployer
git clone git://github.com/mosabua/maven-android-sdk-deployer.git
-
Set up the environment variable
ANDROID_HOME
to contain the Android SDK path -
Install Maven version of Android platform 19
cd /path/to/maven-android-sdk-deployer/platforms/android-19 mvn install -N
-
Install Maven version of google-play-services
cd /path/to/maven-android-sdk-deployer/extras/google-play-services mvn install -N
-
Install Maven version of compatibility-v4
cd /path/to/maven-android-sdk-deployer/extras/compatibility-v4 mvn install -N
-
Install Maven version of compatibility-v7-appcompat
cd /path/to/maven-android-sdk-deployer/extras/compatibility-v7-appcompat mvn install -N
First, you must register the application with Google Cloud Messaging for Android and enable access to the Google Cloud Messaging for Android APIs and Google APIs. This ensures access to the APIs by the JBoss Unified Push Server when it routes push notification requests from the application to the GCM. Registering an application with GCM requires that you have a Google account.
- Log into the Google Cloud Console
- In the
Projects
view, clickCreate Project
. - In the
PROJECT NAME
field type a project name, select the check box to agree to the Terms of Service and clickCreate
. - Reload the page and in the
Projects
view click the project you just created. Make note of theProject Number
. - Click
APIs and auth
>APIs
and change the status ofGoogle Cloud Messaging for Android
toON
. - Click
APIs and auth
>Credentials
and underPublic API access
clickCreate new Key
. - Click
Server Key
and clickCreate
. Make note of theAPI KEY
.
Second, you must register the application and an Android variant of the application with the JBoss Unified Push Server. This requires a running JBoss Unified Push Server OpenShift instance and uses the unique metadata assigned to the application by GCM. For more information about deploying, configuring and using the JBoss Unified Push Server, see the JBoss Unified Push documentation and JBoss xPaaS Services for OpenShift.
- Log into the JBoss Unified Push Server OpenShift instance console.
- In the
Applications
view, clickCreate Application
. - In the
Name
andDescription
fields, type values for the application and clickCreate
. - When created, under the application click
No variants
. - Click
Add Variant
. - In the
Name
andDescription
fields, type values for the Android application variant. - Click
Android
and in theGoogle Cloud Messaging Key
andProject Number
fields type the values assigned to the project by GCM. - Click
Add
. - When created, expand the variant name and make note of the
Server URL
,Variant ID
, andSecret
.
The project source code must be customized with the unique metadata assigned to the application variant by the JBoss Unified Push Server OpenShift instance and GCM.
-
Open
QUICKSTART_HOME/push-contacts-mobile/client/push-contacts-mobile-android/src/org/jboss/aerogear/unifiedpush/quickstart/Constants.java
for editing. -
Enter the application variant values allocated by the JBoss Unified Push Server OpenShift instance and GCM for the following constants:
String UNIFIED_PUSH_URL = "<pushServerURL e.g https://{OPENSHIFT_UNIFIEDPUSHSERVER_URL}/ag-push >"; String VARIANT_ID = "<variantID e.g. 1234456-234320>"; String SECRET = "<variantSecret e.g. 1234456-234320>"; String GCM_SENDER_ID = "<senderID e.g Google Project ID only for android>";
-
Save the file.
-
Build the application
cd QUICKSTART_HOME/push-contacts-mobile/client/push-contacts-mobile-android mvn compile
- The JBoss Unified Push Server OpenShift instance must be running before the application is deployed to ensure that the device successfully registers with the JBoss Unified Push Server on application deployment.
- The
push-contacts-mobile/server/push-contacts-mobile-picketlink-secured
application must be running before attempting to log into the mobile client application to ensure successful login. For more information, see the README distributed with thepush-contacts-mobile-picketlink-secured
application.
The application can be tested on physical Android devices only; push notifications are not available for Android emulators. To deploy, run and debug the application on an Android device attached to your system, on the command line enter the following:
cd QUICKSTART_HOME/push-contacts-mobile/client/push-contacts-mobile-android
mvn clean package android:deploy android:run
Application output is displayed in the command line window.
When the application is deployed to an Android device, you can log into it and begin using the CRUD functionality. Note that access to the application is restricted to users registered with the server-side application and to assist you in getting started a number of default users are preconfigured.
-
Launch the application on the Android device.
-
Log into the application using one of the default user credentials ('maria:maria','dan:dan', or 'john:john').
After successful login, you are presented with a list of existing Contacts residing on the server.
-
Click a contact to open the Edit screen where you can modify the contact's details.
You can send a push notification to your device by creating a new Contact.
You can also send a push notification to your device using the push-contacts-mobile/client/contacts-mobile-webapp
application by completing the following steps (for more information, see the README distributed with the contacts-mobile-webapp
application):
- Open the web interface of the
push-contacts-mobile/client/contacts-mobile-webapp
application in a browser at the following URL: http://localhost:8080/jboss-contacts-mobile-webapp/ . - Add a new Contact.
This automatically triggers a push notification request to the JBoss Unified Push Server OpenShift instance and subsequently the push notification displays on the mobile device.