title: android Notification - how to not show in foreground
body: <p>hey all i got a tough problem and need advice.  I have constructed a notification manually after recieving a FCM data payload.  This is how the notification gets created both in foreground and background since its a data payload:</p>
  
  <pre><code>public class MyFirebaseMessagingService extends FirebaseMessagingService {
  
  @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
  
  
      // Check if message contains a data payload.
      if (remoteMessage.getData().size() &gt; 0) {
  
          String msg = remoteMessage.getData().get("message");
          sendNotification(msg);
      }
  
  private PendingIntent createIntent(String msg) {
      Intent intent = new Intent(this, SportsActivity.class);
      Bundle b = new Bundle();
          b.putString(Constants.KEY_GO_TO_TAB, Constants.KEY_DASHBOARD_HOCKEY_SCORE_TAB);
      intent.putExtras(b);
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
              PendingIntent.FLAG_ONE_SHOT);
      return pendingIntent;
  }
  
  private void sendNotification( String messageBody) {
      Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
      android.support.v4.app.NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
              .setSmallIcon(R.drawable.notification_icon)
              .setStyle(new NotificationCompat.BigTextStyle()
                      .bigText(messageBody))
              .setContentText(messageBody)
              .setColor(ContextCompat.getColor(this, R.color.hockey_brand))
              .setAutoCancel(true)
              .setSound(defaultSoundUri)
              .setContentIntent(createIntent(messageBody));
  
      NotificationManager notificationManager =
              (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  
      notificationManager.notify(0, notificationBuilder.build());
  }
  </code></pre>
  
  <p>}</p>
  
  <p>it seems to function fine. The issue im having is i want the notification to NOT SHOW in when the app is in the foreground. is there not a way to do this without scanning the activity task ? as this requires a permission</p>
  
  <p>i've read <a href="https://stackoverflow.com/questions/4414171/how-to-detect-when-an-android-app-goes-to-the-background-and-come-back-to-the-fo">this article</a> but this how to know you've  went from foreground to background.  also  android.permission.GET_TASKS is deprecated and REAL_GET_TASKS permission is not for third party either. I simply want to know at any given time that the user is either in foreground or background so i know if i should show a notification or not.  I wonder if firebase itself has something.  When you send a "Notification payload" from the firebase console if the app is not in the foreground is does not show in the notification panel so there should be a way to do this.  </p>