Author: Daniel Passos (dpassos)
Level: Beginner
Technologies: Java, Android
Summary: The push-helloworld-android
quickstart shows how to use the JBoss Unified Push Android Push plug-in to register and receive push notifications.
Target Product: JBoss Unified Push
Versions: 1.0
Source: https://github.com/jboss-developer/jboss-mobile-quickstarts/
The push-helloworld-android
quickstart demonstrates how to include basic push functionality in Android applications using the JBoss Unified Push Android Push plug-in.
This simple project consists of a ready-to-build Android application. Before building the application, you must register the Android variant of the application with a running JBoss Unified Push Server OpenShift instance and Google Cloud Messaging for Android. The resulting unique IDs and other parameters must then be inserted into the application source code. After this is complete, the application can be built and deployed to Android devices.
When the 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.
- 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-helloworld-android/src/org/jboss/aerogear/unifiedpush/helloworld/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-helloworld-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 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-helloworld-android
mvn clean package android:deploy android:run
Application output is displayed in the command line window.
You can send a push notification to your device using the JBoss Unified Push Server console by completing the following steps:
- Log into the JBoss Unified Push Server OpenShift instance console.
- Click
Send Push
. - From the
Applications
list, select the application. - In the
Messages
field, type the text to be sent as the push notification. - Click
Send Push Notification
.
RegisterActivity
is invoked right after a successful application login. The Activity life cycle onCreate
is called first invoking the register
method — attempting to register the application to receive push notifications.
PushConfig config = new PushConfig(new URI(UNIFIED_PUSH_URL), GCM_SENDER_ID);
config.setVariantID(VARIANT_ID);
config.setSecret(SECRET);
Registrations registrations = new Registrations();
PushRegistrar registrar = registrations.push("register", config);
registrar.register(getApplicationContext(), new Callback<Void>() {
@Override
public void onSuccess(Void data) {
Toast.makeText(getApplicationContext(),
getApplicationContext().getString(R.string.registration_successful),
Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
finish();
}
});
Before the usage of GCM notifications on Android, we need to include some permissions for GCM and a broadcast receiver to handle push messages from the service.
To enable the permissions we add these as child of the manifest element.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="com.mypackage.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="org.jboss.aerogear.unifiedpush.helloworld" />
And add this element as a child of the application element, to register the default JBoss Unified Push for Android broadcast receiver. It will receive all messages and dispatch the message to registered handlers.
<receiver
android:name="org.jboss.aerogear.android.unifiedpush.AeroGearGCMMessageReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="org.jboss.aerogear.unifiedpush.helloworld" />
</intent-filter>
</receiver>
All push messages are received by an instance of AeroGearGCMMessageReceiver
. They are processed and passed to Registrations via the notifyHandlers
method.
The NotificationBarMessageHandler
is able to receive that message and show it in the Notification Bar.
In the MessagesActivity
we need to remove the handler when the Activity goes into the background and re-enable it when it comes into the foreground.
@Override
protected void onResume() {
super.onResume();
Registrations.registerMainThreadHandler(this);
Registrations.unregisterBackgroundThreadHandler(NotificationBarMessageHandler.instance);
}
@Override
protected void onPause() {
super.onPause();
Registrations.unregisterMainThreadHandler(this);
Registrations.registerBackgroundThreadHandler(NotificationBarMessageHandler.instance);
}