title: FCM - Setting badge in onMessageReceived
body: <p>I have an Android application, where I'm using some method to show notification number on app icon. Now I want to set that number when notification is received.</p>
  
  <p>I thought that I should set the number when notification received so I set it inside <code>onMessageReceived</code> method. But, my problem is <strong>when my app is in background, <code>onMessageReceived</code> method not called, so the notification number isn't set.</strong></p>
  
  <p>Following is my code. I set the number inside <code>onMessageReceived</code>. I already tested <code>setBadge</code> method and can verify that it is working. The problem is <code>onMessageReceived</code> is not called so <code>setBadge</code> is also not called, which doesn't set the number.</p>
  
  <pre><code>@Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
  
      // TODO(developer): Handle FCM messages here.
      Log.d(TAG, "From: " + remoteMessage.getFrom());
      Conts.notificationCounter ++;
      //I am setting in here.
      setBadge(getApplicationContext(),Conts.notificationCounter  );
      Log.e("notificationNUmber",":"+ Conts.notificationCounter);
  
      // 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());
      }
  
      // Also if you intend on generating your own notifications as a result of a received FCM
      // message, here is where that should be initiated. See sendNotification method below.
  }
  // [END receive_message]
  
  
  
  public static void setBadge(Context context, int count) {
      String launcherClassName = getLauncherClassName(context);
      if (launcherClassName == null) {
          Log.e("classname","null");
          return;
      }
      Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
      intent.putExtra("badge_count", count);
      intent.putExtra("badge_count_package_name", context.getPackageName());
      intent.putExtra("badge_count_class_name", launcherClassName);
      context.sendBroadcast(intent);
  }
  
  public static String getLauncherClassName(Context context) {
  
      PackageManager pm = context.getPackageManager();
  
      Intent intent = new Intent(Intent.ACTION_MAIN);
      intent.addCategory(Intent.CATEGORY_LAUNCHER);
  
      List&lt;ResolveInfo&gt; resolveInfos = pm.queryIntentActivities(intent, 0);
      for (ResolveInfo resolveInfo : resolveInfos) {
          String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
          if (pkgName.equalsIgnoreCase(context.getPackageName())) {
              String className = resolveInfo.activityInfo.name;
              return className;
          }
      }
      return null;
  }
  </code></pre>
  
  <p>When I searched this <a href="https://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase/37845174">issue</a>, I found that if the coming message is <em>display message</em> then <code>onMessageReceived</code> is called only if app is foreground. But if coming message is <em>data message</em> then <code>onMessageReceived</code> is called even if the app is background.</p>
  
  <p>But my friend told me who is sending the notification(server side), the message already goes as both display and data message. He said that data object is filled.</p>
  
  <p>Following is the <strong>JSON</strong> for <strong>coming message</strong>, it has <strong>data object</strong>.</p>
  
  <pre><code>{  
     "to":"my_device_id",
     "priority":"high",
  
     "notification":{  
        "body":"Notification Body",
        "title":"Notification Title",
        "icon":"myicon",
        "sound":"default"
     },
  
     "data":{  
        "Nick":"DataNick",
        "Room":"DataRoom"
     }
  }
  </code></pre>
  
  <p>If I only use data object, onMessageReceived is called as they said but that time notification does not appear at the top.</p>
  
  <p>Now why <code>onMessageReceived</code> is not called if the message is also data message. Should I do something different to handle data message? Is it working same with display messaging in client side. </p>
  
  <p>Any help would be appreciated. Thanks in advance.</p>