Feed on
Posts

## How to ignore GCM messages from Urban Airship

I recently tried Urban Airship to manage cross-platform mobile push messages. Even if it's a very good service, it tries to deal with different cloud messaging systems (GCM, APNS, etc) in a uniform way and in doing that it forces users to use all services in the same way, as if they have the same features.
On the contrary, they have more differences than similarities and, for example, its standard library for Android introduces a basic approach layer, which hides some great GCM features, such as the possibility to ignore a message.

The Urban Airship documentation explains very well how to integrate their lib into your project but it completely lacks examples on how you can extend it or modify its basic behaviour. So I wrote some code to explain how to extend their GCMPushReceiver standard receiver to add a business logic layer which controls messages when they arrive and decides if they have to be notified or not.

After you've added references to the Google Play services SDK, you have to write your own GcmBroadcastReceiver that extends their GCMPushReceiver putting some logic to discard messages to be ignored. For example I wrote a receiver that discards all messages which contain the 'ignore' key set to 'true':

JAVA:
1. package mypackage;
2.
3. import android.app.Activity;
4. import android.content.Context;
5. import android.content.Intent;
6. import android.util.Log;
7.
10.
13.
14.     @Override
15.     public void onReceive(Context context, Intent intent) {
17.         String messageType = gcm.getMessageType(intent);
18.         Log.d(TAG, "GCM Message Type: " + messageType);
19.         Log.d(TAG, "Received: " + intent.getExtras().toString());
20.
21.         if ("true".equals(intent.getExtras().getString("ignore"))) {
22.             setResultCode(Activity.RESULT_OK);
23.             return;
24.         }
26.     }
27. }

Then you have to replace in the AndroidManifest.xml the default Urban Airship receiver with the one you wrote with something like the following:

XML: