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 -  https://cloud.google.com/console
Login with your gmail account


# Create New Project
Create New Project



Complet the Mobile number verification for security



# Get SENDER_ID
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



Done!!

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
/extras/google/google_play_servies/libproject
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.



Done!!

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
<application
        <receiver
            
android:name=".GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" ]]>
            
<intent-filter]]>
                
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
                
<category android:name="" />
            </intent-filter]]>

        </receiver]]>
     
<application/>

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

# 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 com.google.android.gms.common.internal.safeparcel.SafeParcelable {
    public static final *** NULL;
}

-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
    @ccom.google.android.gms.common.annotation.KeepName *;
}

-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

@Override
protected void onCreate(Bundle savedInstanceState)
{

     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_launch);
     context = getApplicationContext();
         if (checkPlayServices()) 
     {
            
gcm = GoogleCloudMessaging.getInstance(this);
            
regid = getRegistrationId(context);

            
if (regid.isEmpty())
            {
                registerInBackground();
            }
            
else
            {
            Log.d(
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)) {
                GooglePlayServicesUtil.getErrorDialog(resultCode, 
this,
                        
PLAY_SERVICES_RESOLUTION_REQUEST).show();
            } 
else {
                Log.d(
TAG"This device is not supported - Google Play Services.");
                finish();
            }
            
return false;
        }
        
return true;
 }

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

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

private SharedPreferences getGCMPreferences(Context context) 
{
    
return getSharedPreferences(LaunchActivity.class.getSimpleName(),
                Context.
MODE_PRIVATE);
}

private static int getAppVersion(Context context) 
{
     
try 
     {
         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() {
     
Override
     
protected Object doInBackground(Object... params) 
     {
          String msg = 
"";
          
try 
          {
               
if (gcm == null
               {
                        
gcm = GoogleCloudMessaging.getInstance(context);
               }
               
regid = gcm.register(SENDER_ID);               Log.d(TAG"########################################");
               Log.d(
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 };
  }.execute(nullnullnull);
}

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

REGISTRATION ID
API Key
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();
               data.putString(
"my_message""Hello World");
               data.putString(
"my_action",
               
"com.google.android.gcm.demo.app.ECHO_NOW");
               String id = Integer.toString(msgId.incrementAndGet());
               gcm.send(SENDER_ID + "@gcm.googleapis.com", id, data);
               msg = 
"Sent message";
           } 
catch (IOException ex) {
                 msg = 
"Error :" + ex.getMessage();
           }
           
return msg;
     }
     @Override
     
protected void onPostExecute(String msg) {
          mDisplay.append(msg + 
"\n");
     }
  }.execute(
nullnullnull);
}


6. Receive Push Notifications

#Add GCM Broadcast Receiver Class

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

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

}

#Add GCM Service Class

As we have already declared “GcmBroadcastReceiver.java” 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() {
     
super("GcmIntentService");     
     }     @Override
     protected void onHandleIntent(Intent intent) {
          Bundle extras = intent.getExtras();
          Log.d(
TAG"Notification Data Json :" + extras.getString("message"));

          GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(
this);
          String messageType = gcm.getMessageType(intent);
          if (!extras.isEmpty()) {          if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
               .equals(messageType)) {
               sendNotification(
"Send error: " + extras.toString());
          } 
else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
          .equals(messageType)) {
          sendNotification(
"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++) {
               Log.d(
TAG," Working... " + (i + 1) + "/5 @ "
               + SystemClock.elapsedRealtime());               try {
                    Thread.sleep(5000);
               } 
catch (InterruptedException e) {
               }
             }
             Log.i(
TAG"Completed work @ " + SystemClock.elapsedRealtime());
             sendNotification(extras.getString(
"message"));
           }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
     }
     // 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
          .getSystemService(Context.
NOTIFICATION_SERVICE);
          PendingIntent contentIntent = PendingIntent.getActivity(
this, 0,          new Intent(this, LaunchActivity.class), 0);

          NotificationCompat.Builder mBuilder = 
new NotificationCompat.Builder(          this)
          .setSmallIcon(R.drawable.
icon)
          .setContentTitle(
"Ocutag Snap")
          .setStyle(
new NotificationCompat.BigTextStyle().bigText(msg))
          .setContentText(msg)
          .setDefaults(Notification.
DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);

          mBuilder.setContentIntent(contentIntent);
          mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
     }

}



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.

Images:





Reviews:



More Details: http://www.ibtimes.co.in/articles/522715/20131116/google-nexus-5-release-indian-store-29990.htm

Cheers!!
-RDC

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: http://download.eclipse.org/birt/update-site/4.3     |
----------------------------------------------------------------------------

Source: http://wiki.eclipse.org/BIRT_Update_Site_URL
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: http://download.eclipse.org/mat/1.3/update-site/  |
----------------------------------------------------------------------------

Source:http://www.eclipse.org/mat/downloads.php
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
http://android-developers.blogspot.in/2011/03/memory-analysis-for-android.html
http://2min2code.com/articles/eclipse_memory_analyzer/overview
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.mat.ui.help%2Fgettingstarted%2Fbasictutorial.html
http://eclipsesource.com/blogs/2013/01/21/10-tips-for-using-the-eclipse-memory-analyzer/