I was facing issues of getting notification in background. So I made a simple app to test fcm with more detail cases. I want to share the test cases with you. It may helps you. I am not 100% accurate If I made any mistake please correct me. I will leave this repo as open source If you want to add more details into. Thanks for reading.
If the fcm notificaiton has inculdes data & notification both and the app is in foreground then you will recieve both (data and notificaiton) in onMessageReceived there you can handle them.
If the fcm notificaiton has inculdes data & notification both and the app is in background then you will recieve only notification which is handled by FCM itself. You would not recieve anything in onMessageReceived.
If you click on notification then It will lunch **luncher activity **. If the fcm notificaiton request has data along with notificaiton. and you need the values inside data payload then you can get them from intent of luncher activity e-g
Inside onCreate of Luncher Activity ->** getInent().getStringExtra("test");** "test" is a key value pair inside data payload. In your case write the matched key which you have put in data payload.
If app is killed then you might not recieve notificaiton. Sometimes It happens when you killed app and send notification to device then nothing happen on your device.
Extended fcm-test-case3 - If the fcm-test-case3 happens then you open the app and closed it. Send fcm notificaiton then you will start recieving notificaitons, fresh and pending notifications as well (In fcm-test-case3 not recieved notificaitons which was in pending state)
Extended fcm-test-case2 - (repeating) If the fcm notificaiton has inculdes data & notification both and the app is in background then you will recieve only notification which is handled by FCM itself. You would not recieve anything in onMessageReceived.
Now If you remove notification payload from request and left only data then you will get data payload in onMessageReceived and notificaiton will null If the app is in background.
If the app is in foreground then you will get only data and notification will null because we didn't inculde notification payload to fcm request.
If the app is killed you will recieve data payload in onMessageReceived there you can handle it with cutsom notificaiton to notify user and can get data values to perform your actions.
Now If you remove data payload from request and left only notificaiton then you will get notificaiton payload in onMessageReceived and data will null.
If the app is in background then you will not recieve notification payload in onMessageReceived. you will recieve only notification which is handled by FCM itself.
If the app is killed you will recieve only notification which is handled by FCM itself. If you click on notificaiton it will redirect to luncher activity.