Android – Notification Based Popup System

Notification delivery via your Android application is a feature in high demand. Luckily to meet this demand Google provides a free service by the name of Google Cloud Messaging (GCM).

gcm-logo

Implementing GCM in your Android application already has great tutorials out there so I wont go reinventing the wheel. Our team used Google’s GCM documentation, and as the mobile developer responsible for the client side implementation I followed the Implementing GCM Client guide.

Lets start from where you’ve already implemented a push notification system, and your Android application can receive data from your server via GCM. This article is about adding some icing to that delicious cake of yours, by offering an alternative behavior and some features to those posted notifications.

Push Notification System Requirements

To add some context our requirements were. On clicking the delivered notification in the users notification area. If the application was in the:

  • Background – Bring the application to the foreground opening the applications first activity (commonly know as the MainActivity) displaying a popup containing the notifications full message.
  • Foreground – Opens the applications current activity displaying a popup containing the notifications full message.

After some research I came across a question on Stack Overflow that both asked and answered how to implement what I required. The answer was to use pending intents. My addition to that answer is that this pending intent should contain multiple intents. Now lets see exactly how to do it.

Push Notification System Amendments

A key part of a working GCM system is an intent service AKA GcmIntentService in Google’s GCM documentation. It operates in the background listening for intents from a broadcast receiver AKA GcmBroadcastReceiever in the Google’s GCM documentation. If you’ve followed Google’s GCM documentaion you’ll have just that. Its the following intent service class written by Google that we will be amending.

 

More specifically  we will be amending the sendNotification method. Find the amended code below.

The main amendment here is to the pending intent attached to the push notification. Rather than passing a single intent we pass an ‘intent bundle’ (as I like to call it) of two intents. The first intent opens the application simulating the Android OS’s main launcher (I think). This is achieved with two flags:

  • Intent.ACTION_MAIN
  • Intent.CATEGORY_LAUNCHER

Once the application has been started/resumed the second intent opens up another activity on top of your applications history stack. Here I’ve called that activity DialogActivity (how to implement a dialog themed activity is out of the scope of this article so I leave that as an activity for you). I recommend that the notificationIntent that launches the DialogActivity adds the notification message to the activity Bundle Instance which the activity can extract and display.

Additional Features

Here are a few more features I recommend you add:

  1. Add a unique identifier for each notification.
  2. Change the DialogActivity to use a singleTop launch mode.
  3. Set a ticker message.
  4. Add the autoCancel property (notification removes itself from notification area after it has been clicked) to the notifications.
  5. Set the phone to play a sound, display light or vibrate on receiving a notification.

Find below the following code that adds all the properties.

To address each of the five features:

  1. NOTIFICATION_ID is incremented by one every time a notification is posted. I recommend you find a better solution. I sure did.
  2. The notificationIntent is give an an additional flag Intent.FLAG_ACTIVITY_SINGLE_TOP to give the DialogAcitivity singleTop behaviour.
  3. A ticker message is added using the .setTicker(msg) method.
  4. The autoCancel property is added with the .autoCancel(true) method.
  5. The notification is given all the default values where applicable which includes vibrate, sound and light display with the .setDefaults(Notification.DEFAULT_ALL) method.

Fin

So there you have it. You’ve just finished implementing a non evil notification based popup system. Enjoy!

P.S – I’m no Android guru nor do I claim to be. So if you see something I’ve done wrong or that could be improved, please let me know in the comments section below.

One thought on “Android – Notification Based Popup System

Leave a Reply

Your email address will not be published.

3 + 6 =