title: Google Cloud Messaging register method provides a null value
body: <p>As recommended in the <a href="https://developer.android.com/google/gcm/client.html" rel="nofollow noreferrer">available documentation</a> I decided to implement an automatic update whenever there is an update of the version of my application.</p>
  
  <p>For doing that I have a service that is running in the background performing several operations appart from the GCM update. This service is calling a class that performs all operations related to GCM.</p>
  
  <p>So, basically, this is the call to performed in the Service:</p>
  
  <pre><code>      try {
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            currentVersion = info.versionCode;
        } catch (NameNotFoundException e) {
              //Handle exception
        }
  
        if (registeredVersion != currentVersion) {
            Log.i(ApplicationData.APP_TAG, TAG + ": New version, updating");
            GcmUpdater upGcm = new GcmUpdater(getApplicationContext());
            Boolean update = upGcm.getAndUpdate();
            //We update the current version
            if (update) {
                prefs.setAppPrevVersion(currentVersion);
            } else {
                Log.e(ApplicationData.APP_TAG, TAG + ": GCM not updated");
  
            }
  
        } else {
  
            Log.i(ApplicationData.APP_TAG, TAG + ": Same version, no GCM needed");
        }
  </code></pre>
  
  <p>Ok, I think the key point in the previous code is that I am initiating the class called GcmUpdater is initiated using the application context given by the service.</p>
  
  <p>The constructor of my class GcmUpdater is the following:</p>
  
  <pre><code>public GcmUpdater(Context cont) {
      context = cont;
      TAG = getClass().getName();
      prefs = new StorePreferences(context);
  }
  </code></pre>
  
  <p>Nothing special, as you can see I am calling the method inside GcmUpdater called getAndUpdate(), this method is the following one</p>
  
  <pre><code>public Boolean getAndUpdate() {
      String new_regid = giveRegId();
      return updateGCM(new_regid);
  }
  </code></pre>
  
  <p>Ok, the problem is coming now, is the public function giveRegId()</p>
  
  <pre><code>public String giveRegId() {
  
      try{
          return new RegisterGCM().execute().get();
      }catch(Exception ex){
          ex.printStackTrace();
          return null;
      }
  
  }
  </code></pre>
  
  <p>Which calls to the asyncronous task RegisterGCM....</p>
  
  <pre><code>public class RegisterGCM extends AsyncTask&lt;Void,Void,String&gt;
  {       
      @Override
      protected String doInBackground(Void... arg0)
      {
          try {
              if (gcm == null) {
                  gcm = GoogleCloudMessaging.getInstance(context);
              }
              regid = gcm.register(ApplicationData.SENDER_ID);
              Log.i(ApplicationData.APP_TAG, TAG +":Device registered, registration ID=" + regid);
  
          } catch (IOException ex) {
              Log.e(ApplicationData.APP_TAG, TAG + ": " + ex.getMessage());                  
          }
          return regid;
      }
  
      protected void onPostExecute(Boolean result) {
          return ;
      }
  
  }
  </code></pre>
  
  <p>The problem I am facing is that the variable regid obtained is null and according to similar problems like <a href="https://stackoverflow.com/questions/20820079/google-cloud-messaging-register-fails">this one</a> or <a href="https://stackoverflow.com/questions/23239425/google-cloud-messaging-register-gives-null-pointer-error">this other one,</a> I should include the ApplicationContext, however that is passed as parameter in the constructor.</p>
  
  <p>Moreover, the class RegisterGCM is used by my main activity and works. So my guess has been always that the way to call to register the GCM code is the one that is creating the problem, but is not clear why.</p>
  
  <p>What am I doing wrong? I have not been able to find any explication of this problem in google. </p>