How to handle notification when app in background in Firebase
<p>Here is my manifest:</p>
<pre><code>&lt;service android:name=&quot;.fcm.PshycoFirebaseMessagingServices&quot;&gt;
    &lt;intent-filter&gt;
        &lt;action android:name=&quot;com.google.firebase.MESSAGING_EVENT&quot; /&gt;
    &lt;/intent-filter&gt;
&lt;/service&gt;

&lt;service android:name=&quot;.fcm.PshycoFirebaseInstanceIDService&quot;&gt;
    &lt;intent-filter&gt;
        &lt;action android:name=&quot;com.google.firebase.INSTANCE_ID_EVENT&quot; /&gt;
    &lt;/intent-filter&gt;
&lt;/service&gt;
</code></pre>
<p>When the app is in the background and a notification arrives, then the default notification comes and doesn't run my code of <code>onMessageReceived</code>.</p>
<p>Here is my <code>onMessageReceived</code> code. This is invoked if my app is running on the foreground, not when it is running in the background. How can I run this code when the app is in background too?</p>
<pre><code>// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO(developer): Handle FCM messages here.
    // If the application is in the foreground handle both data and notification messages here.
    // 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.
    data = remoteMessage.getData();
    String title = remoteMessage.getNotification().getTitle();
    String message = remoteMessage.getNotification().getBody();
    String imageUrl = (String) data.get(&quot;image&quot;);
    String action = (String) data.get(&quot;action&quot;);
    Log.i(TAG, &quot;onMessageReceived: title : &quot;+title);
    Log.i(TAG, &quot;onMessageReceived: message : &quot;+message);
    Log.i(TAG, &quot;onMessageReceived: imageUrl : &quot;+imageUrl);
    Log.i(TAG, &quot;onMessageReceived: action : &quot;+action);

    if (imageUrl == null) {
        sendNotification(title,message,action);
    } else {
        new BigPictureNotification(this,title,message,imageUrl,action);
    }
}
// [END receive_message]
</code></pre>