title: GCM Message is not sent when the app is not open?
body: <p>Recently I implement the GCM on my app. I followed the tutorial code at this website</p>
  
  <p><a href="http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/" rel="nofollow">http://javapapers.com/android/google-cloud-messaging-gcm-for-android-and-push-notifications/</a></p>
  
  <p>However, the behavior is quite strange , for example</p>
  
  <p>If I kill the app, then when I send the notification, it will not show on the notification bar immediately , but only when the user open the app again, say, if I send two notification when I kill app, I receive two notification only when I open the app.</p>
  
  <p>Is it the behavior of GCM? As I expect it should be something like the whatsapp (even I did not open app, the device should still receive the notification and display)</p>
  
  <p>here is my code. Thanks for helping</p>
  
  <p><strong>GcmBroadcastReceiver</strong> </p>
  
  <pre><code>public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
          ComponentName comp = new ComponentName(context.getPackageName(),
                  GCMNotificationIntentService.class.getName());
          startWakefulService(context, (intent.setComponent(comp)));
          setResultCode(Activity.RESULT_OK);
      }
  }
  </code></pre>
  
  <p><strong>GCMNotificationIntentService</strong></p>
  
  <pre><code>public class GCMNotificationIntentService extends IntentService {
  
      public static final int NOTIFICATION_ID = 1;
      private NotificationManager mNotificationManager;
      NotificationCompat.Builder builder;
  
      public GCMNotificationIntentService() {
          super("GcmIntentService");
      }
  
      public static final String TAG = "GCMNotificationIntentService";
  
      @Override
      protected void onHandleIntent(Intent intent) {
          Bundle extras = intent.getExtras();
          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());
              } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
  
                  for (int i = 0; i &lt; 3; i++) {
                      Log.i(TAG, "Working... " + (i + 1) + "/5 @ " + SystemClock.elapsedRealtime());
                      try {
                          Thread.sleep(5000);
                      } catch (InterruptedException e) {
                      }
                  }
  
                  Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                  sendNotification(getResources().getString(R.string.gcm_news_remind));
                  Log.i(TAG, "Received: " + extras.toString());
              }
          }
          GcmBroadcastReceiver.completeWakefulIntent(intent);
      }
  
      private void sendNotification(String msg) {
          Log.d(TAG, "Preparing to send notification...: " + msg);
          mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
  
          PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, SplashScreen.class), 0);
  
          NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                  this).setSmallIcon(R.drawable.ic_launcher)
                  .setContentTitle(getResources().getString(R.string.app_name))
                  .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                  .setContentText(msg);
  
          mBuilder.setContentIntent(contentIntent);
          mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
          Log.d(TAG, "Notification sent successfully.");
      }
  }
  </code></pre>
  
  <p><strong>Manifest</strong></p>
  
  <pre><code>&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.oshpedia"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&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.example.oshpedia.permission.C2D_MESSAGE" /&gt;
      &lt;uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
      &lt;uses-permission android:name="android.permission.VIBRATE" /&gt;
  
      &lt;permission
          android:name="com.example.oshpedia.permission.C2D_MESSAGE"
          android:protectionLevel="signature" /&gt;
  
      &lt;uses-sdk
          android:minSdkVersion="9"
          android:targetSdkVersion="19" /&gt;
  
      &lt;application
          android:name=".Defination.MyApp"
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:largeHeap="true"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name=".Activity.SplashScreen"
              android:label="@string/app_name"
              android:theme="@android:style/Theme.NoTitleBar.Fullscreen" &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;activity
              android:name=".Activity.Main"
              android:screenOrientation="portrait"
              android:windowSoftInputMode="stateHidden|adjustPan" &gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".Activity.AboutUs"
              android:screenOrientation="portrait" &gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".Activity.AboutApp"
              android:screenOrientation="portrait" &gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".Activity.Disclaimer"
              android:screenOrientation="portrait" &gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".Activity.EnquiryForm"
              android:screenOrientation="portrait"
              android:windowSoftInputMode="stateHidden|adjustPan" &gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".Activity.NewsDetail"
              android:screenOrientation="portrait" &gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".Activity.EnquiryDetail"
              android:screenOrientation="portrait" &gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".Activity.SearchResult"
              android:screenOrientation="portrait" &gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".Activity.LawDetail"
              android:screenOrientation="portrait" &gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".Activity.AdvanceSearch"
              android:screenOrientation="portrait" &gt;
          &lt;/activity&gt;
          &lt;activity
              android:name=".Activity.VideoChannel"
              android:screenOrientation="portrait" &gt;
          &lt;/activity&gt;
          &lt;activity android:name=".Activity.VideoViewer" &gt;
          &lt;/activity&gt;
  
          &lt;receiver
              android:name=".GCM.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;action android:name="com.google.android.c2dm.intent.REGISTRATION" /&gt;
  
                  &lt;category android:name="com.example.oshpedia" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
  
          &lt;service android:name=".GCM.GCMNotificationIntentService" /&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>