Register for GCM registration id in android application and receive and show notification

So after learning GCM process flow and how to create a project on Google Developer Console now its time to learn how to register device and android app on GCM and get registration id. This registration id is then used to send notification message on GCM server and GCM server delivers this message to our android device.

So first thing we need to do is install Google Play Services using Android SDK Manager. You can find Google Play Services under Extras heading in android SDK manager.

Setup Google Play Service

After installing google play service we have to follow below steps to start using google play services in our project

  1. Import google-play-services_lib project from C:Program Files (x86)Androidandroid-sdkextrasgooglegoogle_play_serviceslibproject (or C:Program FilesAndroidandroid-sdkextrasgooglegoogle_play_serviceslibproject) to your work space.
  2. Right click on your project in package explorer of Eclipse IDE and go toProperties -> Android -> Library -> Add. Now select google-play-services_lib project and click ok.Again click ok to close project properties. Now we are ready to start coding.

Implement Google Play Service

Now we are ready to start implementing notification service in Android application
  1. Add import statement for and in your


  2. Add a TextView and a ProgressBar in your activity layout
    <TextView android:text="@string/registering_message" android:layout_width="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium" />
    android:id="@+id/registrationProgressBar" />


  3. Add below object declaration in your MainActivity
    private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
    private BroadcastReceiver mRegistrationBroadcastReceiver;
    private ProgressBar mRegistrationProgressBar;
    private TextView mInformationTextView;


  4. Initialize BroadcastReceiver object and override OnReceive action. Use below code in your OnCreate method of MainActivity to do so
          mRegistrationProgressBar = (ProgressBar) findViewById(;
    mRegistrationBroadcastReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
    SharedPreferences sharedPreferences =                       PreferenceManager.getDefaultSharedPreferences(context);
    boolean sentToken = sharedPreferences                      .getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false);
    if (sentToken) {
    } else {
    mInformationTextView = (TextView) findViewById(;
  5. Override two methods (onResult and OnPause) and define one more method (checkPlayService).
    protected void onResume() {
     LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver, new IntentFilter(QuickstartPreferences.REGISTRATION_COMPLETE));
    protected void onPause() {
    private boolean checkPlayServices() {
    GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
    int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
    if (resultCode != ConnectionResult.SUCCESS) {
    if (apiAvailability.isUserResolvableError(resultCode)) {
    apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST).show();
    else {
    Log.i(TAG, "This device is not supported.");
    return false;
    return true;


  6. We are done with the MainActivity and layout. Now we need to add four classes in our application package- MyGcmListenerService extended from GcmListenerService – Download
    – MyInstanceIDListenerService extended from InstanceIDListenerService – Download
    – QuickstartPreferences for static final string to use with SharedPreferences – Download
    – RegistrationIntentService extended from IntentService – Download
  7. Update AndroidManifest.xmlAdd Permission
    <uses-permission android:name="" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />

    Add meta data tag. This is very important

    <meta-data android:name=""  android:value="@integer/google_play_services_version" />

    Add receiver and services

    <!-- [START gcm_receiver] -->
                android:permission="" >
                    <action android:name="" />
                    <category android:name="" />
            <!-- [END gcm_receiver] -->
    <!-- [START gcm_listener] -->
    android:exported="false" >
    <action android:name="" />
    <!-- [END gcm_listener] -->
    <!-- [START instanceId_listener] -->
    <action android:name=""/>
    <!-- [END instanceId_listener] -->
Now we are done with all needed code and configuration. Just run app on device and get the registration id in logs.

Leave a Reply