title: GCM: messages by IntentService.sentBroadcast are not received if application is not visible
body: <p>I'm trying to implement a GCM client &amp; server architecture. Everything works fine so far.</p>
  
  <p>Except: When my activity is closed and I get a new notification by GCM, the notification is displayed in the notification bar. So far, so good. But when I click on the notification, my activity is opened but the <code>onReceive</code> event of my <code>BroadcastReceiver</code> is not triggered. :(
  If the Activity is open, the <code>onReceive</code> is triggered perfectly.</p>
  
  <p>Do you know, what's wrong here?</p>
  
  <p>Cheers </p>
  
  <p>Chris</p>
  
  <p>So this is my service:</p>
  
  <pre><code>package xy;
  
  import ...;
  
  public class GcmIntentService extends IntentService
  {
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
  
  
    public GcmIntentService()
    {
      super("GcmIntentService");
    }
  
  
    @Override
    protected void onHandleIntent(Intent intent)
    {
      Bundle extras = intent.getExtras();
      GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
      String messageType = gcm.getMessageType(intent);
  
      if (!extras.isEmpty()) { // has effect of unparcelling Bundle
  
       final int notificationID = (int) (Math.random() * 100000000);
  
       if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
          sendNotification("GCM notification: Send error", extras.toString(), notificationID);
       } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
         sendNotification("Deleted messages on server", extras.toString(), notificationID);
        // If it's a regular GCM message, do some work.
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
         sendNotification(extras.getString(Utils.TICKER_TITLE_MESSAGE_KEY), extras
              .getString(Utils.TICKER_TEXT_MESSAGE_KEY), notificationID);
          Intent intentToBroadCast = new Intent(Utils.DISPLAY_MESSAGE_ACTION);
          intentToBroadCast.putExtra(Utils.MESSAGE_EXTRA_BUNDLE_KEY, extras);
          intentToBroadCast.putExtra(Utils.NOTIFICATION_ID_KEY, notificationID);
          sendBroadcast(intentToBroadCast);
        }
      }
      // Release the wake lock provided by the WakefulBroadcastReceiver.
      GcmBroadcastReceiver.completeWakefulIntent(intent);
    }
  
  
    private void sendNotification(final String aTitle, final String aText, final int aNotificationID)
    {
      mNotificationManager = (NotificationManager) this
          .getSystemService(Context.NOTIFICATION_SERVICE);
  
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this,
          DemoActivity.class), 0);
  
      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon(
          R.drawable.ic_stat_gcm).setContentTitle(aTitle).setStyle(
          new NotificationCompat.BigTextStyle().bigText(aText)).setContentText(aText);
  
      mBuilder.setContentIntent(contentIntent);
      mNotificationManager.notify(aNotificationID, mBuilder.build());
    }
  }
  </code></pre>
  
  <p>And here's the Receiver in my activity, which is to display the incoming message:</p>
  
  <pre><code>public class GcmResultReceiver extends BroadcastReceiver
  {
    @Override
    public void onReceive(Context context, Intent intent)
    {
      Bundle extras = intent.getBundleExtra(Utils.MESSAGE_EXTRA_BUNDLE_KEY);
      String s = extras.getString(Utils.CONTENT_TITLE_MESSAGE_KEY) + "\n"
          + extras.getString(Utils.CONTENT_TEXT_MESSAGE_KEY);
      mDisplay.setText(s);
  
      int notificationID = intent.getIntExtra(Utils.NOTIFICATION_ID_KEY, -1);
      if (-1 != notificationID) m_SentNotificationIDs.add(notificationID);
  
      if (m_IsVisible) {
        clearNotifications();
      }
    }
  };
  </code></pre>
  
  <p>Everything was copied and adapted from the GCM example from the Google Android tutorial.</p>