title: Firebase onMessageReceived(RemoteMessage remoteMessage), is not called when the app is in background
body: <p>In Firebase <code>onMessageReceived(RemoteMessage remoteMessage)</code>, this method will be called when your app is in Foreground. So, on clicking the notification you can open a different Activity, say <code>NotiicationActivity</code>.</p>
  
  <p>But what if your app is in background, then this method will not be called, and on clicking the notification only a Launcher Activity will be opened. </p>
  
  <p>So how to open the <code>NotificationActivity</code> on clicking the notification even if our app is in background. </p>
  
  <p>My Code is this :</p>
  
  <pre><code>public class MyFirebaseMessagingService extends FirebaseMessagingService {
  
      private static final String TAG = "MyFirebaseMsgService";
  
      @Override
      public void onMessageReceived(RemoteMessage remoteMessage) {
          Log.d(TAG, "From: " + remoteMessage.getFrom());
  
          if (remoteMessage.getData().size() &gt; 0) {
              Log.d(TAG, "Message data payload: " + remoteMessage.getData());
          }
  
          if (remoteMessage.getNotification() != null) {
              Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
          }
  
          sendNotification(remoteMessage.getNotification().getBody());
  
      }
  
      private void sendNotification(String messageBody) {
          Intent intent = new Intent(this, NewActivity.class);
          intent.putExtra("key", messageBody);
          intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
              PendingIntent.FLAG_ONE_SHOT);
          Bitmap icon2 = BitmapFactory.decodeResource(getResources(),
              R.mipmap.ic_launcher);
  
          Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
          NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
          .setSmallIcon(R.mipmap.ic_launcher)
          .setContentTitle("FCM Sample")
          .setContentText(messageBody)
          .setAutoCancel(true)
          .setLargeIcon(icon2)
          .setSound(defaultSoundUri)
          .setContentIntent(pendingIntent);
  
          NotificationManager notificationManager =
          (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  
          notificationManager.notify(new Random().nextInt() /* ID of notification */, notificationBuilder.build());
      }
  }
  </code></pre>