Thursday, November 28, 2013

Getting Started with Android Push Notification : Latest Google Cloud Messaging (GCM) - step by step complete tutorial

GCM is a free service that helps developers send data from servers to their Android applications on Android devices, and upstream messages from the user's device back to the cloud.

1. Create/Register App on Google Cloud
2. Setup Cloud SDK with Development
3. Configure project for GCM
4. Get Device Registration ID
5. Send Push Notifications
6. Receive Push Notifications
7. Handle Further step as per Notification type. 

1. Create and Register Application on Google Cloud

Go to the Google Cloud Console -
Login with your gmail account

# Create New Project
Create New Project

Complet the Mobile number verification for security

Once you create Project, you will get landed on google cloud’s dash board
Here you can see SENDER_ID
Note : please note down SENDER_ID it is important to get RegID from GCM

Enable Google cloud messaging service
From Dashboard go to —> APIs & auth
Switch ON - Google Cloud Messaging for Android option

# Get API key
select API & auth  —> Registered apps
click Register App button

Give the details this way

If you are Sending Push Notification From your Local Web Server

Name - MyCloud App
Platform -  [ Android   : Accessing APIs via a web server ]
Click Register.

If you are Sending Push Notification From your Android App itself

Name - MyCloud App
Platform -  [ Android   : Accessing APIs directly from Android ]
Package Name -
SHA1 fingerprint -

Check here How to Generate SHA key for your app

Hint : Just search for the string “To find your SHA-1 fingerprint:" and follow the instructions

Click Register.

Once it get registered —> click on Server Key  —> copy API key

Note : please note down API_KEY , it is important for sending PN Message to GCM


2. Setup Cloud SDK with Development

Google Cloud Messaging require Google Play Service SDK installed in our Development

# Install Google Play Service SDK
Go to Android SDK manager —> Extras 
Install Google Play Service SDK
Install Google Play Service for Froyo SDK (If your app supports android 2.2)

# Update Android Support Library
Go to Android SDK manager —> Extras
Install or Update Android Support Library 

# Import Google Play Service SDK into our Project
Go to path where Android SDK is installed in your system
copy the library project named “google-play-services_lib” from location
and paste where you maintain your lib projects
Now come to project —> Import —>Android —> Existing Android code in Workspace

# Add Google Play Service Lib to build path
Right click on project —> Build Path —> Configure Build Path
Click on Android —> Is Library —> Add —>Apply.


3. Configure project for GCM 

# Update AndroidManifest file
For enable GCM in our project we need to add few permission in our manifest file
Go to AndroidManifest.xml and add below code
# Add Permission
<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="android.permission.VIBRATE" />

<uses-permission android:name=.permission.RECEIVE" />
<uses-permission android:name=“<your_package_name_here>.permission.C2D_MESSAGE" />
<permission android:name=“<your_package_name_here>.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

# Add GCM Broadcast Receiver declaration

add GCM Broadcast Receiver declaration in your application tag
            android:permission="" ]]>
<action android:name="" />
<category android:name="" />


# Add GCM Servie declaration
     <service android:name=".GcmIntentService" />

# Update Proguard Config File
If you are using Proguard in your application then you must configure 
Go to proguard-project.txt File and add below code

-keep class * extends java.util.ListResourceBundle {
    protected Object[][] getContents();

-keep public class {
    public static final *** NULL;

-keepnames class *
-keepclassmembernames class * { *;

-keepnames class * implements android.os.Parcelable {
    public static final ** CREATOR;

4. Get Device Registration ID

Now When device launch the app we need to Get the Registration ID to Send Message

So Go to your Launch/Splash Activity 

#Add Constants and Class Variables

private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;public static final String EXTRA_MESSAGE = "message";public static final String PROPERTY_REG_ID = "registration_id";private static final String PROPERTY_APP_VERSION = "appVersion";private final static String TAG = "LaunchActivity";
protected String SENDER_ID = "your_sender_id";private GoogleCloudMessaging gcm =null;private String regid = null;
private Context contextnull;

# Update OnCreate and OnResume methods

protected void onCreate(Bundle savedInstanceState)

     context = getApplicationContext();
         if (checkPlayServices()) 
gcm = GoogleCloudMessaging.getInstance(this);
regid = getRegistrationId(context);

if (regid.isEmpty())
TAG"No valid Google Play Services APK found.");


@Override protected void onResume()
super.onResume();       checkPlayServices();

# Implement GCM Required methods (Add below methods in LaunchActivity)

private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
else {
TAG"This device is not supported - Google Play Services.");
return false;
return true;

private String getRegistrationId(Context context) 
final SharedPreferences prefs = getGCMPreferences(context);
   String registrationId = prefs.getString(
if (registrationId.isEmpty()) {
TAG"Registration ID not found.");
return "";

int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
int currentVersion = getAppVersion(context);
if (registeredVersion != currentVersion) {
TAG"App version changed.");
return "";
return registrationId;

private SharedPreferences getGCMPreferences(Context context) 
return getSharedPreferences(LaunchActivity.class.getSimpleName(),

private static int getAppVersion(Context context) 
         PackageInfo packageInfo = context.getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionCode;
      catch (NameNotFoundException e) 
throw new RuntimeException("Could not get package name: " + e);


private void registerInBackground() 
{     new AsyncTask() {
protected Object doInBackground(Object... params) 
          String msg = 
if (gcm == null
gcm = GoogleCloudMessaging.getInstance(context);
regid = gcm.register(SENDER_ID);               Log.d(TAG"########################################");
TAG"Current Device's Registration ID is: "+msg);     
          catch (IOException ex) 
              msg = 
"Error :" + ex.getMessage();
          return null;
     protected void onPostExecute(Object result) 
     { //to do here };

Note : please store REGISTRATION_KEY, it is important for sending PN Message to GCM
also keep in mine this will be unique for all device, by using this only GCM will send Push Notification.

5. Send Push Notifications

# If you are sending message from your local web server

While launching/login app you need to send REGISTRATION_ID to your web server.
Actually Web server needs below three variable to send Message to GCM

Message Text

# If you are sending message from your Android app itself 

here you need to add below code in your activity to send Message 

public void onClick(final View view) 
new AsyncTask() {     @Override
     protected String doInBackground(Void... params) 
          String msg = 
"";          try {
               Bundle data = 
new Bundle();
"my_message""Hello World");
               String id = Integer.toString(msgId.incrementAndGet());
               gcm.send(SENDER_ID + "", id, data);
               msg = 
"Sent message";
catch (IOException ex) {
                 msg = 
"Error :" + ex.getMessage();
return msg;
protected void onPostExecute(String msg) {
          mDisplay.append(msg + 

6. Receive Push Notifications

#Add GCM Broadcast Receiver Class

As we have already declared “” in our Manifest file, So lets create this class
update receiver class code this way

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
public void onReceive(Context context, Intent intent) 
    {        ComponentName comp = new ComponentName(context.getPackageName(),
class.getName());        startWakefulService(context, (intent.setComponent(comp)));
“wow!! received new push notification", Toast.LENGTH_LONG).show();


#Add GCM Service Class

As we have already declared “” in our Manifest file, So lets create this class
update receiver class code this way

public class GcmIntentService extends IntentService
     public static final int NOTIFICATION_ID = 1;     private NotificationManager mNotificationManager;     private final static String TAG = "GcmIntentService";     public GcmIntentService() {
     }     @Override
     protected void onHandleIntent(Intent intent) {
          Bundle extras = intent.getExtras();
TAG"Notification Data Json :" + extras.getString("message"));

          GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(
          String messageType = gcm.getMessageType(intent);
          if (!extras.isEmpty()) {          if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
               .equals(messageType)) {
"Send error: " + extras.toString());
else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
          .equals(messageType)) {
"Deleted messages on server: "
          + extras.toString());
          // If it's a regular GCM message, do some work.
else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
          .equals(messageType)) {

          // This loop represents the service doing some work.
          for (int i = 0; i < 5; i++) {
TAG," Working... " + (i + 1) + "/5 @ "
               + SystemClock.elapsedRealtime());               try {
catch (InterruptedException e) {
TAG"Completed work @ " + SystemClock.elapsedRealtime());
        // Release the wake lock provided by the WakefulBroadcastReceiver.
     // Put the message into a notification and post it.
     // This is just one simple example of what you might choose to do with
     // a GCM message.
     private void sendNotification(String msg) {          mNotificationManager = (NotificationManager) this
          PendingIntent contentIntent = PendingIntent.getActivity(
this, 0,          new Intent(this, LaunchActivity.class), 0);

          NotificationCompat.Builder mBuilder = 
new NotificationCompat.Builder(          this)
"Ocutag Snap")
new NotificationCompat.BigTextStyle().bigText(msg))



That’s It We have Done!! no more coding. Just Build the App
If you sending Message from your own Web server tell them to send Message
or else you can send message from your own device.

Run the app. And you should Receive the Push Notification on Your Device with Sound + Vibration alert.

Cheers!! We Did It.

For more info please! Check below references : 

Google Documentation

Google Open Source Code

Monday, November 18, 2013

LG- Google Nexus 5 - Released : October 31, 2013

Google Nexus 5  : If you are a nexus fan, Google is all set to launch its much awaited Nexus 5 Smartphone in India October 31, 2013.
With several retailers across metro cities confirming the arrival of stocks by Monday, 18th November,
Price for 16GB Nexus 5 price quoted to be Rs. 28,900 ($349) and 32GB price to be Rs. 32,999 ( $399).

Tech Specs:

Model - LG- Google Nexus 5
Camera front - 8.0-megapixel , back - 1.3-megapixel
Screen 4.95 inch, 1920x1080 display (445 ppi)
Qualcomm quad-core 2.26GHz processor
Network -3G And 4G/LTE and Dualband Wi-Fi
Android™ 4.4, KitKat®
Wireless charging
Battery -Talk time: up to 17 hours
  Standby mode: 300 hours.



More Details:


Thursday, November 14, 2013

How to check Memory Leaks using Eclipse Memory Analyzer Tool (MAT) in Android App

Eclipse Memory Analyzer Tool (MAT)
Java handles its memory in two areas. The heap and the stack.

Java Heap
In the heap the Java Virtual Machine (JVM) stores all objects created by the Java application, e.g. by using the "new" operator. The Java garbage collector (gc) can logically separate the heap into different areas, so that the gc can faster identify objects which can get removed
The memory for new objects is allocated on the heap at run time. Instance variables live inside the object in which they are declared.

Java Stack
Stack is where the method invocations and the local variables are stored. If a method is called then its stack frame is put onto the top of the call stack. The stack frame holds the state of the method including which line of code is executing and the values of all local variables. The method at the top of the stack is always the current running method for that stack. Threads have their own call stack.

Install MAT on Eclipse
We need to install two eclipse plugins
1. BIRT Chart Engine  //For Getting Memory Info with Pie Chart diagrams
2. MAT                        //For Getting Memory Info
Install BIRT Chart Engine plugins
1. Eclipse —> Help Menu —> Install New Software —> Add
2. Enter below information
|    Name : BIRT Chart Engine                                                     |
|    Location:     |

Now select only Birt Chart Engine and Go for Install.

Install MAT plugins
1. Eclipse —> Help Menu —> Install New Software —> Add
2. Enter below information
|    Name : MAT                                                                          |
|    Location:  |

Now select MAT and Birt Chart Plugin for MAT and Go for Install.
Get Heap Dump of Android Project
Once you installed ..
1. Connect Android Device for Debugging/Testing App
2. Now Go the The project and Build/Run
3. Open DDMS —> Devices —> Select Process (Project’s Package Name in List)
4. Click the "Dump HPROF File" icon in the Devices view's toolbar (looks like a half-filled can with a downward-pointing arrow)

5. Select the option Leak Suspects Report and click Finish.
6. That’s it!!.. You Got the App’s Memory Heap Report

Analyse Memory Leaks, Allocation & GC etc.

Heap Overview

Few Important Links