FirebaseMessagingService does not persist in memory after app is closed [duplicate] - android

I have read a similar question on SO, however, I was not able to get the correct answer from it.
I have a system wherein we send notification to around 500 devices.
Unfortunately, many of these devices are not receiving the notification. I have found that OPPO F1 series phones are particularly not getting the notification.
I have observed that this occurs if the app is stopped from multi-task tray. How do I resolve this?
Update: I have observed that when I close the app from task-tray, my app is forced stop in application manager. While when I close Whatsapp from task-tray, it is still not forced stop. How is that being handled by Whatsapp?

Update 03/2017 - Including a part of my answer here.
For the topic with regards to swipe closed/killed/force stopped, this topic has been discussed for quite some time and there doesn't seem to be a definite answer. During one of my testings, I am able to still receive a message (tested with a data-only message payload) if I Swipe close my app. But when I force closed it from the Settings menu, I wasn't able to receive any messages. Do note that this is not always the behavior.
There are some devices that were designed that when you swipe close the app, it will be the same as force stopping them (see my answer here).
There are also devices where even if the app is still just simply swiped away, even though it's not force closed, the device itself is preventing it from receiving messages. Others say that this can't be the case because apps like WhatsApp were able to do it. The reason I've learned so far for that is because the device manufacturers have whitelisted most of the well-known apps for it to be possible.
This is not documented anywhere because (IMO), this is a topic that depends also on the device and that FCM has no total control over.
Original Answer:
Since it's device specific (as you mentioned in your post: OPPO F1 series phones), it may very well be possible that when an app is stopped from multi-task tray in that device, it is actually killing the app, causing the services and other background processes associated with it to also be destroyed. See this answer for a little more idea of what I'm trying to say.
If you search around the community, what is commonly suggested here is to make use of the START_STICKY flag. However, I've seen that it was previously mentioned before for FirebaseMessagingService (see this post, comment by #ArthurThompson):
These services will be started by Google Play services, which is always running on the device. You don't have to and should not start/stop these services yourself.
With that said, there is also the possibility of (again from the comments):
There may be a setting on the device that allows/disallows this.
I suggest doing further testing if the services are being killed by the device itself or see if there are settings that are blocking the notifications.

Have you tried to use stopWithTask attribute on your service class?
<service
android:name="com.yourapp.YourPushService"
android:stopWithTask="false" />
If set to true, this service with be automatically stopped when the
user remove a task rooted in an activity owned by the application. The
default is false.
If the flag is false, there is an onTaskRemoved callback in your Service class.
In the case you can detect the "swipe" event, and you can implement a workaround.

I've been through the same but in my case, it was Xiaomi phones instead of Oppo phones. What actually happens is that when you close the app from system tray, the system kills the app entirely. What that means is your app won't be able to receive notifications via GCM/FCM. WAKE_LOCK permission doesn't help either.
That does NOT mean that phone is not receiving the notification. It is. It just won't let the apps show it. You can verify this by sending a broadcast from adb and looking at your logcat.
One possible solution to this problem is to use SyncAdapter. Although it is NOT advised, I've seen some apps using it. Other possible solutions are to use some kind of background service which is always running. Some people also use AlarmManager as it almost never gets killed. My point is - you cannot rely on GCM/FCM for your notifications.
Let's talk about WhatsApp now -
In Xiaomi phones, they whitelist or blacklist an app based on certain criteria. If you download an app and if it is in their whitelist, they'll permit the app to show notifications. If not, you already know what happens. But the good thing is that you can change these settings. Look for an app named Security. If you revoke the right permissions, even WhatsApp will stop showing notifications.

I was also facing the same issue, But then I realized after lots of debugging that, i was stopping the services that receive the Firebase notifications in on stop method of one of the activities.
Please check whether you are stopping these services anywhere in the app.
Make sure you are using service and not intent-service.
Swiping the app will never stop services. So try to debug the app for first two point.

Answer was found here
There are no way to send data message from notification console.
But there are other way to send notification to devices and them will be catch inside onMessageReceived!
You need can use terminal (Mac or Linux) or some service like Postman to send Post request on this link: https://fcm.googleapis.com/fcm/send
with the next body:
{
"to": "/topics/your_topic_here",
"data": {
"text":"text",
"text1":"text1",
...
}
}
also you need to add 2 headers:
Authorization - key=your_server_key_here
Content-Type - application/json
To get your server key, you can find it in the firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key

I am using MoEngage Push notification service to send push notifications.
The solution is to initialise the PushNotification object/service in Application class of Android , instead of MainActivity.
Then notifications will be received in killed state as well.
How to call from Application class
Declare the class name which will be the Application class inside application tag in your androidManifest.xml file
<application
android:name="App" //class name that will be an Application class
android:label="#string/app_name"
android:fullBackupContent="#xml/backup_descriptor"
android:usesCleartextTraffic="true"
android:icon="#mipmap/ic_launcher">
This will be the App.kt class
class App: FlutterApplication() {
override fun onCreate() {
super.onCreate()
//initialize your notification service here
}
}

Related

FCM notification not received when the app is swiped or force closed [duplicate]

I have read a similar question on SO, however, I was not able to get the correct answer from it.
I have a system wherein we send notification to around 500 devices.
Unfortunately, many of these devices are not receiving the notification. I have found that OPPO F1 series phones are particularly not getting the notification.
I have observed that this occurs if the app is stopped from multi-task tray. How do I resolve this?
Update: I have observed that when I close the app from task-tray, my app is forced stop in application manager. While when I close Whatsapp from task-tray, it is still not forced stop. How is that being handled by Whatsapp?
Update 03/2017 - Including a part of my answer here.
For the topic with regards to swipe closed/killed/force stopped, this topic has been discussed for quite some time and there doesn't seem to be a definite answer. During one of my testings, I am able to still receive a message (tested with a data-only message payload) if I Swipe close my app. But when I force closed it from the Settings menu, I wasn't able to receive any messages. Do note that this is not always the behavior.
There are some devices that were designed that when you swipe close the app, it will be the same as force stopping them (see my answer here).
There are also devices where even if the app is still just simply swiped away, even though it's not force closed, the device itself is preventing it from receiving messages. Others say that this can't be the case because apps like WhatsApp were able to do it. The reason I've learned so far for that is because the device manufacturers have whitelisted most of the well-known apps for it to be possible.
This is not documented anywhere because (IMO), this is a topic that depends also on the device and that FCM has no total control over.
Original Answer:
Since it's device specific (as you mentioned in your post: OPPO F1 series phones), it may very well be possible that when an app is stopped from multi-task tray in that device, it is actually killing the app, causing the services and other background processes associated with it to also be destroyed. See this answer for a little more idea of what I'm trying to say.
If you search around the community, what is commonly suggested here is to make use of the START_STICKY flag. However, I've seen that it was previously mentioned before for FirebaseMessagingService (see this post, comment by #ArthurThompson):
These services will be started by Google Play services, which is always running on the device. You don't have to and should not start/stop these services yourself.
With that said, there is also the possibility of (again from the comments):
There may be a setting on the device that allows/disallows this.
I suggest doing further testing if the services are being killed by the device itself or see if there are settings that are blocking the notifications.
Have you tried to use stopWithTask attribute on your service class?
<service
android:name="com.yourapp.YourPushService"
android:stopWithTask="false" />
If set to true, this service with be automatically stopped when the
user remove a task rooted in an activity owned by the application. The
default is false.
If the flag is false, there is an onTaskRemoved callback in your Service class.
In the case you can detect the "swipe" event, and you can implement a workaround.
I've been through the same but in my case, it was Xiaomi phones instead of Oppo phones. What actually happens is that when you close the app from system tray, the system kills the app entirely. What that means is your app won't be able to receive notifications via GCM/FCM. WAKE_LOCK permission doesn't help either.
That does NOT mean that phone is not receiving the notification. It is. It just won't let the apps show it. You can verify this by sending a broadcast from adb and looking at your logcat.
One possible solution to this problem is to use SyncAdapter. Although it is NOT advised, I've seen some apps using it. Other possible solutions are to use some kind of background service which is always running. Some people also use AlarmManager as it almost never gets killed. My point is - you cannot rely on GCM/FCM for your notifications.
Let's talk about WhatsApp now -
In Xiaomi phones, they whitelist or blacklist an app based on certain criteria. If you download an app and if it is in their whitelist, they'll permit the app to show notifications. If not, you already know what happens. But the good thing is that you can change these settings. Look for an app named Security. If you revoke the right permissions, even WhatsApp will stop showing notifications.
I was also facing the same issue, But then I realized after lots of debugging that, i was stopping the services that receive the Firebase notifications in on stop method of one of the activities.
Please check whether you are stopping these services anywhere in the app.
Make sure you are using service and not intent-service.
Swiping the app will never stop services. So try to debug the app for first two point.
Answer was found here
There are no way to send data message from notification console.
But there are other way to send notification to devices and them will be catch inside onMessageReceived!
You need can use terminal (Mac or Linux) or some service like Postman to send Post request on this link: https://fcm.googleapis.com/fcm/send
with the next body:
{
"to": "/topics/your_topic_here",
"data": {
"text":"text",
"text1":"text1",
...
}
}
also you need to add 2 headers:
Authorization - key=your_server_key_here
Content-Type - application/json
To get your server key, you can find it in the firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key
I am using MoEngage Push notification service to send push notifications.
The solution is to initialise the PushNotification object/service in Application class of Android , instead of MainActivity.
Then notifications will be received in killed state as well.
How to call from Application class
Declare the class name which will be the Application class inside application tag in your androidManifest.xml file
<application
android:name="App" //class name that will be an Application class
android:label="#string/app_name"
android:fullBackupContent="#xml/backup_descriptor"
android:usesCleartextTraffic="true"
android:icon="#mipmap/ic_launcher">
This will be the App.kt class
class App: FlutterApplication() {
override fun onCreate() {
super.onCreate()
//initialize your notification service here
}
}

Firebase push notification issue in Android when app is closed/killed

Firebase push does not work on some devices when app is closed with only data payload. See this thread: https://github.com/firebase/quickstart-android/issues/41
I know when app is killed by swipe then some OEM kill all the services of the app which directly effect FirebbaseMessagingService and due to this onMessageReceived() method never invoked. I have also tried with high priority FCM but sadly no success. Here are the phones on which I am facing an issue: OnePlus, Lenovo, Huawei.
Currently, I am testing with OnePlus 5, when I change the battery setting to "Don't Optimise" then push notification started working.
I killed the app and run dumpsys package MY-PACKAGE | grep stopped command and I found that app is not stopped. It shows stopped=false. It means app is running.
The concept of push notification is to notify users when app is closed but currently, we are unable to do.
Any suggestion how can I fix this?
FirebaseMessagingService can receive PushNotification even when app is closed/killed.
But there are some problems in the way. The behavior of the apps changes between development and production, and because of the device provider.
The first thing you have to consider, is when the app is in development, if you force close the app (kill-process), FirebaseMessagingService stop being triggered. But this doesn't happen in production, so don't be aware of this if your APK is signed. link to source
The second thing, is that there some Android phone providers that manage processes by them selfs. We can see an example like Huawei phones and their "Protected applications", which make user decide if want to protect the app or not. Only famous apps are protected on installation like WhatApp or Twitter... link to source
At this point, your FirebaseMessagingService should be triggered, but there are other problems related with memory and processes managed by system (OS). Your Service can be canceled because of the time that it's spending to handle the PushNotification. You can find many ways to handle this problem, but the best way, is Firebase JobDispatcher. link to source

How to show notification in android app when data is updated in firebase realtime database console when app is closed from recent opened list? [duplicate]

I have read a similar question on SO, however, I was not able to get the correct answer from it.
I have a system wherein we send notification to around 500 devices.
Unfortunately, many of these devices are not receiving the notification. I have found that OPPO F1 series phones are particularly not getting the notification.
I have observed that this occurs if the app is stopped from multi-task tray. How do I resolve this?
Update: I have observed that when I close the app from task-tray, my app is forced stop in application manager. While when I close Whatsapp from task-tray, it is still not forced stop. How is that being handled by Whatsapp?
Update 03/2017 - Including a part of my answer here.
For the topic with regards to swipe closed/killed/force stopped, this topic has been discussed for quite some time and there doesn't seem to be a definite answer. During one of my testings, I am able to still receive a message (tested with a data-only message payload) if I Swipe close my app. But when I force closed it from the Settings menu, I wasn't able to receive any messages. Do note that this is not always the behavior.
There are some devices that were designed that when you swipe close the app, it will be the same as force stopping them (see my answer here).
There are also devices where even if the app is still just simply swiped away, even though it's not force closed, the device itself is preventing it from receiving messages. Others say that this can't be the case because apps like WhatsApp were able to do it. The reason I've learned so far for that is because the device manufacturers have whitelisted most of the well-known apps for it to be possible.
This is not documented anywhere because (IMO), this is a topic that depends also on the device and that FCM has no total control over.
Original Answer:
Since it's device specific (as you mentioned in your post: OPPO F1 series phones), it may very well be possible that when an app is stopped from multi-task tray in that device, it is actually killing the app, causing the services and other background processes associated with it to also be destroyed. See this answer for a little more idea of what I'm trying to say.
If you search around the community, what is commonly suggested here is to make use of the START_STICKY flag. However, I've seen that it was previously mentioned before for FirebaseMessagingService (see this post, comment by #ArthurThompson):
These services will be started by Google Play services, which is always running on the device. You don't have to and should not start/stop these services yourself.
With that said, there is also the possibility of (again from the comments):
There may be a setting on the device that allows/disallows this.
I suggest doing further testing if the services are being killed by the device itself or see if there are settings that are blocking the notifications.
Have you tried to use stopWithTask attribute on your service class?
<service
android:name="com.yourapp.YourPushService"
android:stopWithTask="false" />
If set to true, this service with be automatically stopped when the
user remove a task rooted in an activity owned by the application. The
default is false.
If the flag is false, there is an onTaskRemoved callback in your Service class.
In the case you can detect the "swipe" event, and you can implement a workaround.
I've been through the same but in my case, it was Xiaomi phones instead of Oppo phones. What actually happens is that when you close the app from system tray, the system kills the app entirely. What that means is your app won't be able to receive notifications via GCM/FCM. WAKE_LOCK permission doesn't help either.
That does NOT mean that phone is not receiving the notification. It is. It just won't let the apps show it. You can verify this by sending a broadcast from adb and looking at your logcat.
One possible solution to this problem is to use SyncAdapter. Although it is NOT advised, I've seen some apps using it. Other possible solutions are to use some kind of background service which is always running. Some people also use AlarmManager as it almost never gets killed. My point is - you cannot rely on GCM/FCM for your notifications.
Let's talk about WhatsApp now -
In Xiaomi phones, they whitelist or blacklist an app based on certain criteria. If you download an app and if it is in their whitelist, they'll permit the app to show notifications. If not, you already know what happens. But the good thing is that you can change these settings. Look for an app named Security. If you revoke the right permissions, even WhatsApp will stop showing notifications.
I was also facing the same issue, But then I realized after lots of debugging that, i was stopping the services that receive the Firebase notifications in on stop method of one of the activities.
Please check whether you are stopping these services anywhere in the app.
Make sure you are using service and not intent-service.
Swiping the app will never stop services. So try to debug the app for first two point.
Answer was found here
There are no way to send data message from notification console.
But there are other way to send notification to devices and them will be catch inside onMessageReceived!
You need can use terminal (Mac or Linux) or some service like Postman to send Post request on this link: https://fcm.googleapis.com/fcm/send
with the next body:
{
"to": "/topics/your_topic_here",
"data": {
"text":"text",
"text1":"text1",
...
}
}
also you need to add 2 headers:
Authorization - key=your_server_key_here
Content-Type - application/json
To get your server key, you can find it in the firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key
I am using MoEngage Push notification service to send push notifications.
The solution is to initialise the PushNotification object/service in Application class of Android , instead of MainActivity.
Then notifications will be received in killed state as well.
How to call from Application class
Declare the class name which will be the Application class inside application tag in your androidManifest.xml file
<application
android:name="App" //class name that will be an Application class
android:label="#string/app_name"
android:fullBackupContent="#xml/backup_descriptor"
android:usesCleartextTraffic="true"
android:icon="#mipmap/ic_launcher">
This will be the App.kt class
class App: FlutterApplication() {
override fun onCreate() {
super.onCreate()
//initialize your notification service here
}
}

Android app not receiving Firebase Notification when app is stopped from multi-task tray

I have read a similar question on SO, however, I was not able to get the correct answer from it.
I have a system wherein we send notification to around 500 devices.
Unfortunately, many of these devices are not receiving the notification. I have found that OPPO F1 series phones are particularly not getting the notification.
I have observed that this occurs if the app is stopped from multi-task tray. How do I resolve this?
Update: I have observed that when I close the app from task-tray, my app is forced stop in application manager. While when I close Whatsapp from task-tray, it is still not forced stop. How is that being handled by Whatsapp?
Update 03/2017 - Including a part of my answer here.
For the topic with regards to swipe closed/killed/force stopped, this topic has been discussed for quite some time and there doesn't seem to be a definite answer. During one of my testings, I am able to still receive a message (tested with a data-only message payload) if I Swipe close my app. But when I force closed it from the Settings menu, I wasn't able to receive any messages. Do note that this is not always the behavior.
There are some devices that were designed that when you swipe close the app, it will be the same as force stopping them (see my answer here).
There are also devices where even if the app is still just simply swiped away, even though it's not force closed, the device itself is preventing it from receiving messages. Others say that this can't be the case because apps like WhatsApp were able to do it. The reason I've learned so far for that is because the device manufacturers have whitelisted most of the well-known apps for it to be possible.
This is not documented anywhere because (IMO), this is a topic that depends also on the device and that FCM has no total control over.
Original Answer:
Since it's device specific (as you mentioned in your post: OPPO F1 series phones), it may very well be possible that when an app is stopped from multi-task tray in that device, it is actually killing the app, causing the services and other background processes associated with it to also be destroyed. See this answer for a little more idea of what I'm trying to say.
If you search around the community, what is commonly suggested here is to make use of the START_STICKY flag. However, I've seen that it was previously mentioned before for FirebaseMessagingService (see this post, comment by #ArthurThompson):
These services will be started by Google Play services, which is always running on the device. You don't have to and should not start/stop these services yourself.
With that said, there is also the possibility of (again from the comments):
There may be a setting on the device that allows/disallows this.
I suggest doing further testing if the services are being killed by the device itself or see if there are settings that are blocking the notifications.
Have you tried to use stopWithTask attribute on your service class?
<service
android:name="com.yourapp.YourPushService"
android:stopWithTask="false" />
If set to true, this service with be automatically stopped when the
user remove a task rooted in an activity owned by the application. The
default is false.
If the flag is false, there is an onTaskRemoved callback in your Service class.
In the case you can detect the "swipe" event, and you can implement a workaround.
I've been through the same but in my case, it was Xiaomi phones instead of Oppo phones. What actually happens is that when you close the app from system tray, the system kills the app entirely. What that means is your app won't be able to receive notifications via GCM/FCM. WAKE_LOCK permission doesn't help either.
That does NOT mean that phone is not receiving the notification. It is. It just won't let the apps show it. You can verify this by sending a broadcast from adb and looking at your logcat.
One possible solution to this problem is to use SyncAdapter. Although it is NOT advised, I've seen some apps using it. Other possible solutions are to use some kind of background service which is always running. Some people also use AlarmManager as it almost never gets killed. My point is - you cannot rely on GCM/FCM for your notifications.
Let's talk about WhatsApp now -
In Xiaomi phones, they whitelist or blacklist an app based on certain criteria. If you download an app and if it is in their whitelist, they'll permit the app to show notifications. If not, you already know what happens. But the good thing is that you can change these settings. Look for an app named Security. If you revoke the right permissions, even WhatsApp will stop showing notifications.
I was also facing the same issue, But then I realized after lots of debugging that, i was stopping the services that receive the Firebase notifications in on stop method of one of the activities.
Please check whether you are stopping these services anywhere in the app.
Make sure you are using service and not intent-service.
Swiping the app will never stop services. So try to debug the app for first two point.
Answer was found here
There are no way to send data message from notification console.
But there are other way to send notification to devices and them will be catch inside onMessageReceived!
You need can use terminal (Mac or Linux) or some service like Postman to send Post request on this link: https://fcm.googleapis.com/fcm/send
with the next body:
{
"to": "/topics/your_topic_here",
"data": {
"text":"text",
"text1":"text1",
...
}
}
also you need to add 2 headers:
Authorization - key=your_server_key_here
Content-Type - application/json
To get your server key, you can find it in the firebase console: Your project -> settings -> Project settings -> Cloud messaging -> Server Key
I am using MoEngage Push notification service to send push notifications.
The solution is to initialise the PushNotification object/service in Application class of Android , instead of MainActivity.
Then notifications will be received in killed state as well.
How to call from Application class
Declare the class name which will be the Application class inside application tag in your androidManifest.xml file
<application
android:name="App" //class name that will be an Application class
android:label="#string/app_name"
android:fullBackupContent="#xml/backup_descriptor"
android:usesCleartextTraffic="true"
android:icon="#mipmap/ic_launcher">
This will be the App.kt class
class App: FlutterApplication() {
override fun onCreate() {
super.onCreate()
//initialize your notification service here
}
}

how does whatsapp service gets restarted even if i force stop app?

I am running whatsapp (we could call it appX from now on) in device A. I go to manage applications -> force close so appX gets closed and i no longer see appX as running services.
Now, after 5 minutes, I send a message from another device 's appX (device B) to device A appX (the one we killed it).
Here are the 2 scenarios i tested :
device A with android 2.1 : it never receives the message, therefore we could say that none of appX services got restarted. It ONLY receives the message if manually the user restarts the app.
device A with android 2.3.6 : for SOME magic reason, no matter how long it's been since appX got killed, as soon as we send the message from device B -> device A gets the message, therefore, appX's service gets restarted. Note : all the time that appX was closed and WITHOUT receiving any notification, i wasn't able to see any running services of appX in manage applications, so this means that this magic service gets restarted as soon as it receives a message/notification
I know it sounds weird, and lot of people will say this is impossible, but again, this has been tested on these 2 devices.
I am trying to accomplish this same behavior, so any help will be appreciated it.
I don't think it's some magic what happens here! It's just Android C2DM (see: https://developers.google.com/android/c2dm/), whereas the app has a registered Receiver for incoming Push Notifications and gets awaken by this message.
Android C2DM is/was available with Android 2.2, that's the reason why you can't see the same behaviour on your device with Android 2.1 up and running.
By the way: As you can see, C2DM is deprecated since June 26th, 2012. So instead of C2DM, one should use GCM (see: http://developer.android.com/guide/google/gcm/gs.html)
Useful Comment: GCM needs available internet connection. You can using any other broadcast receiver such as SMSReceiver for by passing this limitation.
Starting from Android 3.1 (API 12), if an application is force-stopped it will not restart until the user manually runs the app again.
This will happen even if the app contains a Service or an active BroadcastReceiver.
You can find the official documentation here.
i dont have idea about whatsApp service.
But it is possible that after force stop application, restart service of app.
i use START_STICKY service for my chatApp. i have to do same thing so i use START_STICKY service so when my app kill or force stop from setting, after few second my service get restart and i able to login to my xmpp server and get incoming message.
its nothing magical here appX uses push notifications via android GCM platform https://developer.android.com/google/gcm/index.html in GCM the app registers for a braodcast reciever and the broadcast receiver starts the service on getting the push notification.
Android GCM is dependent on google play services that are available on android 2.2 and beyond that therefore you didn't see the message on 2.1 device

Categories

Resources