title: GcmListenerService.onMessageReceived() called only when app is running
body: <p>I am trying to implement GCM. Everything works fine except that the onMessageReceived() method gets called only when the app is running. </p>
  
  <p>I searched for solutions but I am unable to find the case that matches mine. My request body for GCM contains only the "to" and "data" parts, not the "notification" part.</p>
  
  <p>This is the body of the request sent to GCM:</p>
  
  <pre><code>{"data":{"title":"Test","message":"Test Message"},"to":"ffl3Q260K8E:APA91bG90Ra_CyXdmB4ztcCXYKEMKFliZ_MVpkqYnzUW2Xizcem4iknSt9guKDeEbWYl2YwwEKa7kKVllE0mBRzUOGhO5jJfAM6vuzisq3qqe_hJ1Dy-mpFQat4_ErfKRKRQOJvhKi4q"}
  </code></pre>
  
  <p>This is my GcmListenerService:</p>
  
  <pre><code>public class MyGcmListenerService extends GcmListenerService {
  
      private static final String TAG = "MyGcmListenerService";
  
      @Override
      public void onMessageReceived(String from, Bundle data) {
          String message = data.getString("message");
          Log.d(TAG, "From: " + from);
          Log.d(TAG, "Message: " + message);
  
          sendNotification(message);
      }
  
      private void sendNotification(String message) {
          Intent intent = new Intent(this, MainActivity.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                  PendingIntent.FLAG_ONE_SHOT);
  
          Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
          NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                  .setSmallIcon(R.drawable.ic_app_icon)
                  .setContentTitle("GCM Message")
                  .setContentText(message)
                  .setAutoCancel(true)
                  .setSound(defaultSoundUri)
                  .setContentIntent(pendingIntent);
  
          NotificationManager notificationManager =
                  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  
          notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
      }
  }
  </code></pre>
  
  <p>and this is the manifest file:</p>
  
  <p></p>
  
  <pre><code>    &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  
      &lt;permission android:name="com.example.demoapp.permission.C2D_MESSAGE"
          android:protectionLevel="signature" /&gt;
      &lt;uses-permission android:name="com.example.demoapp.permission.C2D_MESSAGE" /&gt;
  
      &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:supportsRtl="true"
          android:theme="@style/AppTheme"&gt;
  
          &lt;receiver
              android:name="com.google.android.gms.gcm.GcmReceiver"
              android:exported="true"
              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.example.demoapp" /&gt;
              &lt;/intent-filter&gt;
          &lt;/receiver&gt;
          &lt;service
              android:name=".services.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=".services.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;service android:name=".services.RegistrationIntentService"/&gt;
          &lt;activity android:name=".MainActivity"&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;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>