title: GCM GetRegistrationId returns empty string/null
body: <p>I,m Developing GCM service for android in eclipse and i followed the basic tutorial of developing it from <a href="http://developer.android.com/google/gcm/gcm.html" rel="nofollow">Here</a>. The problem is that i m getting empty string in return  when i call the getregistrationid of GMRegistrar, As i m new to it, i cannot figure out the problem. Below is code/description of my files.</p>
  
  <p><strong>MainActivity.java:</strong></p>
  
  <pre><code>   package com.google.android.gcm.demo.app;
  
  import android.os.Bundle;
  import android.app.Activity;
  import android.view.Menu;
  
  import com.google.android.gcm.demo.app.R;
  import com.google.android.gcm.GCMRegistrar;
  import com.google.android.gms.common.ConnectionResult;
  import com.google.android.gms.common.GooglePlayServicesUtil;
  import com.google.android.gms.gcm.GoogleCloudMessaging;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.SharedPreferences;
  import android.content.pm.PackageInfo;
  import android.content.pm.PackageManager.NameNotFoundException;
  import android.os.AsyncTask;
  import android.util.Log;
  import android.view.View;
  import android.widget.TextView;
  
  import java.io.IOException;
  import java.util.concurrent.atomic.AtomicInteger;
  
  public class MainActivity extends Activity {
  
       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 static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
  
          /**
           * Substitute you own sender ID here. This is the project number you got
           * from the API Console, as described in "Getting Started."
           */
          String SENDER_ID = "xxxxxxxxxx";
  
          /**
           * Tag used on log messages.
           */
          static final String TAG = "GCM Demo";
  
          TextView mDisplay;
          GoogleCloudMessaging gcm;
          AtomicInteger msgId = new AtomicInteger();
          Context context;
  
          String regid = "ss";
  
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
           mDisplay = (TextView) findViewById(R.id.display);
  
              context = getApplicationContext();
  
              // Check device for Play Services APK. If check succeeds, proceed with GCM registration.
              if (checkPlayServices()) {
                  gcm = GoogleCloudMessaging.getInstance(this);
                  regid = getRegistrationId(context);
  
                  Log.i(TAG, "Now..."+regid);
                  if (regid.equals("")) 
                  {
                      Log.i(TAG, "Registrng Now...");
      //              registerInBackground();
                      String registrationStatus = "";
                      String regId = "";
                      try {
                          // Check that the device supports GCM (should be in a try / catch)
                          GCMRegistrar.checkDevice(context);
  
                          // Check the manifest to be sure this app has all the required
                          // permissions.
                          GCMRegistrar.checkManifest(context);
  
                          // Get the existing registration id, if it exists.
                          regId = GCMRegistrar.getRegistrationId(context);
  
                          if (regId.equals("")) {
  
                              registrationStatus = "Registering...";
  
                              // register this device for this project
                              GCMRegistrar.register(context, SENDER_ID);
                              regId = GCMRegistrar.getRegistrationId(context);
  
                              registrationStatus = "Registration Acquired";
  
                              // This is actually a dummy function. At this point, one
                              // would send the registration id, and other identifying
                              // information to your server, which should save the id
                              // for use when broadcasting messages.
  
                          } 
                          else 
                          {
  
                              registrationStatus = "Already registered";
  
                          }
  
                          Log.i(TAG, "ID IS:......."+regId);
  
  //                      sendRegistrationToServer();
                      } catch (Exception e) {
  
                          e.printStackTrace();
                          registrationStatus = e.getMessage();
  
                      }
  
                      Log.d(TAG, registrationStatus);
  
                  }
              } 
              else {
  
                  Log.i(TAG, "No valid Google Play Services APK found.");
              }
  
      }
  
        @Override
          protected void onResume() {
              super.onResume();
              // Check device for Play Services APK.
              checkPlayServices();
          }
  
          /**
           * Check the device to make sure it has the Google Play Services APK. If
           * it doesn't, display a dialog that allows users to download the APK from
           * the Google Play Store or enable it in the device's system settings.
           */
          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.i(TAG, "This device is not supported.");
                      finish();
                  }
                  return false;
              }
              return true;
          }
  
          /**
           * Stores the registration ID and the app versionCode in the application's
           * {@code SharedPreferences}.
           *
           * @param context application's context.
           * @param regId registration ID
           */
  //      private void storeRegistrationId(Context context, String regId) {
  //          final SharedPreferences prefs = getGcmPreferences(context);
  //          int appVersion = getAppVersion(context);
  //          Log.i(TAG, "Saving regId on app version " + appVersion);
  //          SharedPreferences.Editor editor = prefs.edit();
  //          editor.putString(PROPERTY_REG_ID, regId);
  //          editor.putInt(PROPERTY_APP_VERSION, appVersion);
  //          editor.commit();
  //      }
  
          /**
           * Gets the current registration ID for application on GCM service, if there is one.
           * &lt;p&gt;
           * If result is empty, the app needs to register.
           *
           * @return registration ID, or empty string if there is no existing
           *         registration ID.
           */
          private String getRegistrationId(Context context) {
              final SharedPreferences prefs = getGcmPreferences(context);
              String registrationId = "ss";
              registrationId = prefs.getString(PROPERTY_REG_ID, "");
              Log.i(TAG, "rgid : " + registrationId);
              if (registrationId.equals("ss")) {
                  Log.i(TAG, "33333");
                  Log.i(TAG, "Registration not found.");
                  return "";
              }
              // Check if app was updated; if so, it must clear the registration ID
              // since the existing regID is not guaranteed to work with the new
              // app version.
              int registeredVersion = prefs.getInt(PROPERTY_APP_VERSION, Integer.MIN_VALUE);
              int currentVersion = getAppVersion(context);
              if (registeredVersion != currentVersion) {
                  Log.i(TAG, "App version changed.");
                  return "";
              }
              return registrationId;
          }
  </code></pre>
  
  <p><strong>GcmIntentService.java:</strong></p>
  
  <pre><code>package com.google.android.gcm.demo.app;
  
  import com.google.android.gms.gcm.GoogleCloudMessaging;
  
  import android.app.IntentService;
  import android.app.NotificationManager;
  import android.app.PendingIntent;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Bundle;
  import android.os.SystemClock;
  import android.support.v4.app.NotificationCompat;
  import android.util.Log;
  
  /**
   * This {@code IntentService} does the actual handling of the GCM message.
   * {@code GcmBroadcastReceiver} (a {@code WakefulBroadcastReceiver}) holds a
   * partial wake lock for this service while the service does its work. When the
   * service is finished, it calls {@code completeWakefulIntent()} to release the
   * wake lock.
   */
  public class GcmIntentService extends IntentService {
      public static final int NOTIFICATION_ID = 1;
      private NotificationManager mNotificationManager;
      NotificationCompat.Builder builder;
  
      public GcmIntentService() {
          super("GcmIntentService");
      }
      public static final String TAG = "GCM Demo";
  
  
      @Override
      protected void onHandleIntent(Intent intent) {
          Bundle extras = intent.getExtras();
          GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
          // The getMessageType() intent parameter must be the intent you received
          // in your BroadcastReceiver.
          String messageType = gcm.getMessageType(intent);
  
          if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
              /*
               * Filter messages based on message type. Since it is likely that GCM will be
               * extended in the future with new message types, just ignore any message types you're
               * not interested in, or that you don't recognize.
               */
              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 &lt; 5; i++) {
                      Log.i(TAG, "Working... " + (i + 1)
                              + "/5 @ " + SystemClock.elapsedRealtime());
                      try {
                          Thread.sleep(5000);
                      } catch (InterruptedException e) {
                      }
                  }
                  Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                  // Post notification of received message.
                  sendNotification("Received: " + extras.toString());
                  Log.i(TAG, "Received: " + extras.toString());
              }
          }
          // 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, MainActivity.class), 0);
  
          NotificationCompat.Builder mBuilder =
                  new NotificationCompat.Builder(this)
          .setContentTitle("GCM Notification")
          .setStyle(new NotificationCompat.BigTextStyle()
          .bigText(msg))
          .setContentText(msg);
  
          mBuilder.setContentIntent(contentIntent);
          mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
      }
  }
  </code></pre>
  
  <p><strong>GcmBroadcastReceiver.java</strong></p>
  
  <pre><code>package com.google.android.gcm.demo.app;
  import android.app.Activity;
  import android.content.ComponentName;
  import android.content.Context;
  import android.content.Intent;
  import android.support.v4.content.WakefulBroadcastReceiver;
  /**
   * This {@code WakefulBroadcastReceiver} takes care of creating and managing a
   * partial wake lock for your app. It passes off the work of processing the GCM
   * message to an {@code IntentService}, while ensuring that the device does not
   * go back to sleep in the transition. The {@code IntentService} calls
   * {@code GcmBroadcastReceiver.completeWakefulIntent()} when it is ready to
   * release the wake lock.
   */
  public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
      @Override
      public void onReceive(Context context, Intent intent) {
          // Explicitly specify that GcmIntentService will handle the intent.
          ComponentName comp = new ComponentName(context.getPackageName(),
                  GcmIntentService.class.getName());
          // Start the service, keeping the device awake while it is launching.
          startWakefulService(context, (intent.setComponent(comp)));
          setResultCode(Activity.RESULT_OK);
      }
  }
  </code></pre>
  
  <p><strong>Manifest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!--
    - Copyright 2013 Google Inc.
    -
    - Licensed under the Apache License, Version 2.0 (the "License"); you may not
    - use this file except in compliance with the License. You may obtain a copy
    - of the License at
    -
    - http://www.apache.org/licenses/LICENSE-2.0
    -
    - Unless required by applicable law or agreed to in writing, software
    - distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    - WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    - License for the specific language governing permissions and limitations
    - under the License.
  --&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.google.android.gcm.demo.app" android:versionCode="1" android:versionName="1.0"&gt;
  
      &lt;!-- GCM requires Android SDK version 2.2 (API level 8) or above. --&gt;
      &lt;!-- The targetSdkVersion is optional, but it's always a good practice
           to target higher versions. --&gt;
      &lt;uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/&gt;
  
      &lt;!-- GCM connects to Google Services. --&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  
      &lt;!-- GCM requires a Google account. --&gt;
      &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt;
  
      &lt;!-- Keeps the processor from sleeping when a message is received. --&gt;
      &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  
      &lt;!--
       Creates a custom permission so only this app can receive its messages.
  
       NOTE: the permission *must* be called PACKAGE.permission.C2D_MESSAGE,
             where PACKAGE is the application's package name.
      --&gt;
      &lt;permission android:name="com.google.android.gcm.demo.app.permission.C2D_MESSAGE"
          android:protectionLevel="signature" /&gt;
      &lt;uses-permission android:name="com.google.android.gcm.demo.app.permission.C2D_MESSAGE" /&gt;
  
      &lt;!-- This app has permission to register and receive data message. --&gt;
      &lt;uses-permission
          android:name="com.google.android.c2dm.permission.RECEIVE" /&gt;
  
      &lt;!-- Main activity. --&gt;
      &lt;application
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name" &gt;
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name"
              android:configChanges="orientation|keyboardHidden|screenSize"
                  android:launchMode="singleTop"&gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;!--
            WakefulBroadcastReceiver that will receive intents from GCM
            services and hand them to the custom IntentService.
  
            The com.google.android.c2dm.permission.SEND permission is necessary
            so only GCM services can send data messages for the app.
          --&gt;
          &lt;receiver
              android:name=".GcmBroadcastReceiver"
              android:permission="com.google.android.c2dm.permission.SEND" &gt;
              &lt;intent-filter&gt;
                  &lt;!-- Receives the actual messages. --&gt;
                  &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt;
                   &lt;action android:name="com.google.android.c2dm.intent.REGISTRATION" /&gt;
                  &lt;category android:name="com.google.android.gcm.demo.app" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
          &lt;service android:name=".GcmIntentService" /&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>Plz Help me.Thanks in advance</p>