Push notification works incorrectly when app is on background or not running

Solution 1:

With FCM you specify a POST payload to send to https://fcm.googleapis.com/fcm/send. In that payload you can specify a data or a notification key, or both.

If your payload contains only a data key, your app will handle all push messages itself. E.g. they are all delivered to your onMessageReceived handler.

If your payload contains a notification key, your app will handle push messages itself only if your app is active/in the foreground. If it is not (so it's in the background, or closed entirely), FCM handles showing the notification for you by using the values you put into the notification key payload.

Note that notifications sent from a console (like Firebase console), they always include a notification key.

Looks like you want to be handling the FCM messages yourself so you can customize the notification a bit more etc, so it would be better to not include the notification key in the POST payload, so all push messages are delivered to your onMessageReceived.

You can read more about it here:
Advanced messaging options
Downstream message syntax

Solution 2:

I spent 2 weeks to understand why my application cannot receive data message anymore when it's in background and as strange as it could be, I arrived to the conclusion that Android-Studio 2.1.2 is the problem!

I cannot receive any FCM message anymore in background application

https://github.com/firebase/quickstart-android/issues/89