title: Android push with new Google Cloud Messaging
body: <p>I'm trying to integrate the Google Cloud Messaging in my application.
  When i wan't register the mobile to the server, ih have this error :</p>
  
  <pre><code>    12-10 14:43:36.451: E/GooglePlayServicesUtil(22455): The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.
  12-10 14:43:36.481: I/Appehour:GCM(22455): Registration not found.
  12-10 14:43:36.501: E/AndroidRuntime(22455): FATAL EXCEPTION: AsyncTask #1
  12-10 14:43:36.501: E/AndroidRuntime(22455): Process: com.appehour.jdm, PID: 22455
  12-10 14:43:36.501: E/AndroidRuntime(22455): java.lang.RuntimeException: An error occured while executing doInBackground()
  12-10 14:43:36.501: E/AndroidRuntime(22455):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
  12-10 14:43:36.501: E/AndroidRuntime(22455):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
  12-10 14:43:36.501: E/AndroidRuntime(22455):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
  12-10 14:43:36.501: E/AndroidRuntime(22455):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
  12-10 14:43:36.501: E/AndroidRuntime(22455):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  12-10 14:43:36.501: E/AndroidRuntime(22455):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  12-10 14:43:36.501: E/AndroidRuntime(22455):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  12-10 14:43:36.501: E/AndroidRuntime(22455):    at java.lang.Thread.run(Thread.java:841)
  12-10 14:43:36.501: E/AndroidRuntime(22455): Caused by: java.lang.NullPointerException
  12-10 14:43:36.501: E/AndroidRuntime(22455):    at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:478)
  12-10 14:43:36.501: E/AndroidRuntime(22455):    at android.app.PendingIntent.getBroadcast(PendingIntent.java:467)
  12-10 14:43:36.501: E/AndroidRuntime(22455):    at com.google.android.gms.gcm.GoogleCloudMessaging.d(Unknown Source)
  12-10 14:43:36.501: E/AndroidRuntime(22455):    at com.google.android.gms.gcm.GoogleCloudMessaging.b(Unknown Source)
  12-10 14:43:36.501: E/AndroidRuntime(22455):    at com.google.android.gms.gcm.GoogleCloudMessaging.register(Unknown Source)
  12-10 14:43:36.501: E/AndroidRuntime(22455):    at com.appehour.jdm.TestPush$registerInBackground.doInBackground(TestPush.java:170)
  12-10 14:43:36.501: E/AndroidRuntime(22455):    at com.appehour.jdm.TestPush$registerInBackground.doInBackground(TestPush.java:1)
  12-10 14:43:36.501: E/AndroidRuntime(22455):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
  12-10 14:43:36.501: E/AndroidRuntime(22455):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  12-10 14:43:36.501: E/AndroidRuntime(22455):    ... 4 more
  </code></pre>
  
  <p>This is my AndroidManifest :</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.myApp"
      android:versionCode="13"
      android:versionName="1.3.9" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="19" /&gt;
  
      &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
  
      &lt;!-- Permissions pour le push --&gt;
      &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt;
      &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
      &lt;uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /&gt;
  
  
      &lt;permission android:name="com.myApp.permission.C2D_MESSAGE"
          android:protectionLevel="signature" /&gt;
      &lt;uses-permission android:name="com.myApp.permission.C2D_MESSAGE" /&gt;
  
  
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" 
          &gt;
  
          &lt;activity
              android:name="com.myApp.Main"
              android:label="@string/app_name" &gt; 
          &lt;/activity&gt;
  
  
           &lt;activity 
                android:name="TestPush" &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;!-- Receiver pour le push --&gt;
          &lt;receiver
              android:name=".GcmBroadcastReceiver"
              android:permission="com.google.android.c2dm.permission.SEND" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt;
                  &lt;category android:name="com.myApp" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
          &lt;service android:name=".GCMIntentService" /&gt;
  
  
      &lt;meta-data android:name="com.google.android.gms.version"
             android:value="@integer/google_play_services_version" /&gt;         
  
      &lt;/application&gt;
  &lt;/manifest&gt;
  </code></pre>
  
  <p>And my activity :</p>
  
  <pre><code>   package com.appehour.jdm;
  
  import java.io.IOException;
  import java.util.concurrent.atomic.AtomicInteger;
  
  import android.annotation.SuppressLint;
  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.os.Bundle;
  import android.util.Log;
  import android.widget.TextView;
  
  import com.google.android.gms.common.ConnectionResult;
  import com.google.android.gms.common.GooglePlayServicesUtil;
  import com.google.android.gms.gcm.GoogleCloudMessaging;
  
  @SuppressLint("NewApi")
  public class TestPush 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 String PREFS_GCM = "prefs_gcm";
  private final static 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 = "xxxxxxxxxxxxx";
  
  /**
   * Tag used on log messages.
   */
  static final String TAG_LOGS = "Appehour:GCM";
  
  TextView mDisplay;
  GoogleCloudMessaging gcm;
  AtomicInteger msgId = new AtomicInteger();
  SharedPreferences prefs;
  Context context;
  
  String regid;
  
  
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.test_push);
      mDisplay = (TextView) findViewById(R.id.textViewTestPush);
  
  
      // Check device for Play Services APK.
      if (checkPlayServices()) {
          gcm = GoogleCloudMessaging.getInstance(context);
  
          regid = getRegistrationId(context);
  
          if (regid.isEmpty()) {
             new registerInBackground().execute();
          }
  
      } else {
          Log.i(TAG_LOGS, "No valid Google Play Services APK found.");
      }
  
  
  
  }
  
  // You need to do the Play Services APK check here too.
  @Override
  protected void onResume() {
      super.onResume();
     // 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("Appehour identification Push", "This device is not supported.");
              finish();
          }
          return false;
      }
      return true;
  
  }
  
  /**
   * Gets the current registration ID for application on GCM service.
   * &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 = prefs.getString(PROPERTY_REG_ID, "");
      if (registrationId.isEmpty()) {
          Log.i(TAG_LOGS, "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_LOGS, "App version changed.");
          return "";
      }
      return registrationId;
  }
  
  /**
   * @return Application's {@code SharedPreferences}.
   */
  private SharedPreferences getGCMPreferences(Context context) {
      // This sample app persists the registration ID in shared preferences, but
      // how you store the regID in your app is up to you.
      return getSharedPreferences(PREFS_GCM,Context.MODE_PRIVATE);
  }
  
  /**
   * @return Application's version code from the {@code PackageManager}.
   */
  private static int getAppVersion(Context context) {
      try {
          PackageInfo packageInfo = context.getPackageManager()
                  .getPackageInfo(context.getPackageName(), 0);
          return packageInfo.versionCode;
      } catch (NameNotFoundException e) {
          // should never happen
          throw new RuntimeException("Could not get package name: " + e);
      }
  }
  
  
   private class registerInBackground extends AsyncTask&lt;String, Void, String&gt; {
  
       protected void onPostExecute(Long result) {
  
       }
  
      @Override
      protected String doInBackground(String... params) {
  
  
          String msg = "";
          try {
              if (gcm == null) {
                  gcm = GoogleCloudMessaging.getInstance(context);
              }
  
              regid = gcm.register(SENDER_ID);
              msg = "Device registered, registration ID=" + regid;
              Log.d(TAG_LOGS, "Device registered, registration ID=" + regid);
  
              // You should send the registration ID to your server over HTTP,
              // so it can use GCM/HTTP or CCS to send messages to your app.
              // The request to your server should be authenticated if your app
              // is using accounts.
              sendRegistrationIdToBackend();
  
              // For this demo: we don't need to send it because the device
              // will send upstream messages to a server that echo back the
              // message using the 'from' address in the message.
  
              // Persist the regID - no need to register again.
              storeRegistrationId(context, regid);
          } catch (IOException ex) {
              msg = "Error :" + ex.getMessage();
              Log.e(TAG_LOGS, "Error :" + ex.getMessage());
              // If there is an error, don't just keep trying to register.
              // Require the user to click a button again, or perform
              // exponential back-off.
          }
          return msg;
      }
   }
  
  
  private void sendRegistrationIdToBackend() {
  
  }
  
  private void storeRegistrationId(Context context, String regId) {
      final SharedPreferences prefs = getGCMPreferences(context);
      int appVersion = getAppVersion(context);
      Log.i(TAG_LOGS, "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();
  }
  }
  </code></pre>
  
  <p>I don't understand the problem.
  Thank's if you know the issue.</p>