title: Open activity on firebase notification received in foreground
body: <p>When my application is open and I receive a notification I want to be able to open the activity associated immediately without the need of the user to tap on the notification. </p>
  
  <p>This question is very similar: <a href="https://stackoverflow.com/questions/37554274/open-app-on-firebase-notification-received-fcm">Open app on firebase notification received (FCM)</a></p>
  
  <p>But it opens the app when it is in background, I need to do it when my app is in foreground.</p>
  
  <p>From the <a href="https://firebase.google.com/docs/notifications/android/console-device" rel="noreferrer">firebase documentation</a>:</p>
  
  <blockquote>
    <p>Notifications delivered when your app is in the background. In this
    case, the notification is delivered to the device’s system tray. A
    user tap on a notification opens the app launcher by default. Messages
    with both notification and data payload, both background and
    foreground. In this case, the notification is delivered to the
    device’s system tray, and the data payload is delivered in the extras
    of the intent of your launcher Activity.</p>
  </blockquote>
  
  <p>This is my implmentation of <code>onMessageReceived</code></p>
  
  <pre><code>@Override
      public void onMessageReceived(RemoteMessage remoteMessage) {
  
         // Check if message contains a data payload.
          if (remoteMessage.getData().size() &gt; 0) {
              Log.d(TAG, "Message data payload: " + remoteMessage.getData());
          }
  
          // Check if message contains a notification payload.
          if (remoteMessage.getNotification() != null) {
              Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
              sendNotification( remoteMessage);              
          }     
      }
  
      /**
       * Create and show a simple notification containing the received FCM message.
       *
       * @param remoteMessage FCM message message received.
       */
      private void sendNotification(RemoteMessage remoteMessage) {
          Intent intent = new Intent(this, MyActivity.class);
  
          Map&lt;String, String&gt; hmap ;
          hmap = remoteMessage.getData();
          hmap.get("data_info");
          intent.putExtra("data_info", hmap.get("data_info"));
          intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
  
  
          PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                  PendingIntent.FLAG_ONE_SHOT);
  
      }
  </code></pre>
  
  <p>I am able to get the notification correctly but the activity only starts once I tap the notification on the system tray.</p>
  
  <p>Is there a way to start the activity without tapping the notification while in foreground?</p>
  
  <p>The method <code>onMessageReceived()</code> from the class <code>MyFirebaseMessagingService</code> that <code>extends FirebaseMessagingService</code> is getting called correctly while in foreground, but the activity is not getting started. I have also tried with the flag <code>FLAG_ACTIVITY_NEW_TASK</code> also with no luck. Thanks in advance.</p>