title: Android push notification when app is close
body: <p>I followed the documentation of google to implement push notifications.
  When the app is open works fine, but when I close the app does not receive notifications.</p>
  
  <p>Here is the code:
  Manifest</p>
  
  <pre><code>&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.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  &lt;uses-permission android:name="android.permission.VIBRATE"/&gt;
  &lt;uses-permission android:name="it.bsdsoftware.cnabologna.permission.C2D_MESSAGE" /&gt;
  &lt;uses-permission android:name="android.permission.GET_ACCOUNTS" /&gt;
  &lt;permission
          android:name="it.bsdsoftware.cnabologna.permission.C2D_MESSAGE"
          android:protectionLevel="signature" /&gt;
  
  &lt;meta-data
      android:name="com.google.android.gms.version"
      android:value="@integer/google_play_services_version"/&gt;
  &lt;application&gt;
      ...
      &lt;service android:name=".notifiche.RegistrationIntentService"
               android:exported="false" &gt;
      &lt;/service&gt;
      &lt;service android:name=".notifiche.MyGcmListenerService"
               android:exported="false" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="com.google.android.c2dm.intent.RECEIVE" /&gt;
          &lt;/intent-filter&gt;
      &lt;/service&gt;
      &lt;service android:name=".notifiche.MyInstanceIDListenerService"
               android:exported="false" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="com.google.android.gms.iid.InstanceID" /&gt;
          &lt;/intent-filter&gt;
      &lt;/service&gt;
      &lt;receiver android:name=".notifiche.GcmBroadcastReceiver"
                android:exported="true"
                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.GCM_RECEIVED_ACTION"/&gt;
              &lt;category android:name="it.bsdsoftware.cnabologna" /&gt;
              &lt;action android:name="com.google.android.c2dm.intent.REGISTRATION" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p>RegistrationIntentService.java</p>
  
  <pre><code>public class RegistrationIntentService extends IntentService {
  
      private static final String TAG = "RegIntentService";
  
      public RegistrationIntentService() {
          super(TAG);
      }
  
      @Override
      protected void onHandleIntent(Intent intent) {
          try {
              InstanceID instanceID = InstanceID.getInstance(this);
              String token = instanceID.getToken(getString(R.string.notifiche_push_sender_id), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
              Log.i(TAG, "GCM Registration Token: " + token);
              sendRegistrationToServer(token);
          } catch (Exception e) {
              Log.d(TAG, "Failed to complete token refresh", e);
          }
      }
  
      private void sendRegistrationToServer(String token) {
          OperazioneInvioToken op = new OperazioneInvioToken(token);
          WebServiceTask ws = new WebServiceTask(op, this, false);
          ws.execute();
      }
  
  }
  </code></pre>
  
  <p>MyGcmListenerService.java</p>
  
  <pre><code>public class MyGcmListenerService extends GcmListenerService {
  
      private static final String TAG = "MyGcmListenerService";
  
      @Override
      public void onMessageReceived(String from, Bundle data) {
  
          Bundle notification = data.getBundle("notification");
  
          String title = "";
          String message = "";
  
          if(notification!=null){
              title = notification.getString("title");
              message = notification.getString("body");
          }
  
          NotificationUtility.showNotification(title, message);
          NotificationUtility.setBadge(1);
      }
  }
  </code></pre>
  
  <p>MyInstanceIDListenerService.java</p>
  
  <pre><code>public class MyInstanceIDListenerService extends InstanceIDListenerService {
  
      /**
       * Called if InstanceID token is updated. This may occur if the security of
       * the previous token had been compromised. This call is initiated by the
       * InstanceID provider.
       */
      @Override
      public void onTokenRefresh() {
          // Fetch updated Instance ID token and notify our app's server of any changes (if applicable).
          Intent intent = new Intent(this, RegistrationIntentService.class);
          startService(intent);
      }
  }
  </code></pre>
  
  <p>GcmBroadcastReceiver.java</p>
  
  <pre><code>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(), MyGcmListenerService.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>When the app is open onMessageReceived of GcmListenerService class is called but when the app is closed is not called.</p>
  
  <p>What did I do wrong?
  Thanks</p>