I am trying to open an Activity
when the notification is clicked and below is my code.
Intent intent = new Intent(this.getApplicationContext(),
Thats the intented behaviour. If your app is in background, notification is created by android system which does not have your pendingIntent action.So it does not work. In the foreground case it works because notification is created by your code.
Please check the doc in the below link. https://firebase.google.com/docs/notifications/android/console-device#receive_and_handle_messages