I am working in a application that uses FCM notifications, when app is running (foreground or background) the notification receives but when i clear the app from my recent apps i do not receive any notification i am using FirebaseMessagingService. So i want to create a service that will keep alive myService which is extending FirebaseMessaging service even if app is not running or killed.
4 Answers
I found a reason for this problem when you Release-APK is not from GPlay some mobile companies block services which want to auto start the app. you can do two think first download app from GPlay or in your phone settings disable battery optimization for your app or permit your app auto start.

- 305
- 3
- 8
-
Already removed my app from battery optimization but still same issue. – Muhammad Adeel Shoukat Sep 20 '19 at 07:28
-
if your phone has a auto start up manager also permit your app in that section. for example Asus phones has a battry optimization and a auto app start manager – Amir Abbas Sep 20 '19 at 12:14
-
Nope :) there is no such thing. now i am making a foreground service which will keep my firebaseMessagingService alive. hope this will work. – Muhammad Adeel Shoukat Sep 20 '19 at 12:34
I've tried everything - in my opinion theres no reliable solution to prevent any service from being killed. The only way is to make sure to deliver the notification to the system tray not to the application.
There are 2 types of FCM notifications: Notification message and Data message.
Data messages are delivered to system tray and are always display - even if service is not running.
Notification message looks like:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
}
and triggers method OnMessageReceaved() of FirebaseMessagingService. Many devices (especially Huawei and Xiaomi) try to do everything to kill background services to prevent battery drain. So the FirebaseMessagingService isn't the best way to handle notifications.
Second type is Data message:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
}
}
}
This type is handled by the system tray, so you don't need any of service running to get the notification. Its much more convenient method, but as far i know, it can't be achieved with the console.
You would probably need server API to send Data message.
Read this for more details.

- 1,532
- 3
- 16
- 24
-
I am using a server API to send the data payload only, i have removed notification payload from it. however, it is working on Anroid O emulator but not on my mobile which is Android P so i have come up with the solution to make another service which will let the myservice to invoke. any guidance which service i should use and how to? – Muhammad Adeel Shoukat Sep 20 '19 at 07:26
-
You could try to wake the device after some time with WorkManager https://developer.android.com/topic/libraries/architecture/workmanager/basics – MarkWalczak Sep 20 '19 at 07:36
-
Could you please more clarify how the data will be handled if i do not use FirebaseMessaging service. from where i can get the data from MainActivity? or somewhere else? and how i would post notifications after getting Data payload only. – Muhammad Adeel Shoukat Sep 20 '19 at 07:56
-
In my opinion every service will be killed after some time by your device to protect battery. I would rather try to figure out why there is a problem with the system tray. Mayby you're blocking notiffications with the device settings? – MarkWalczak Sep 20 '19 at 08:13
-
I have checked all the settings that belongs to my app nothing wrong there but i don't know why i am not getting notification :( – Muhammad Adeel Shoukat Sep 20 '19 at 09:36
-
Can you use another device or connect yours to the Android Studio via ADB to see logs? – MarkWalczak Sep 20 '19 at 09:44
When the application is killed. You need to push silent notification (payload without notitication property) to handle in the FirebaseMessagingService
.
You can refer this link: https://firebase.google.com/docs/cloud-messaging/concept-options

- 1,180
- 3
- 16
- 36
-
i am sending only data payload without notification payload but it is still not working. – Muhammad Adeel Shoukat Sep 20 '19 at 07:28
-
@MuhammadAdeelShoukat Did you use any other notification handle? Could you update your code in the question? – Sinh Phan Sep 20 '19 at 07:33
You will need to use Data-Messages from FCM to have control of your notification payload, by using Data-Messages your app's onMessageReceived will be called even if your app is in background or killed, you will have to create a custom notification yourself to show your payload. On the other hand I used Notification-Messages in FCM, they were delivered even if the app was killed.

- 167
- 1
- 8