I'm testing Firebase Push notifications, sending a notification from the Firebase composer panel, and I noticed that if I close the app process from App Information panel, the push notifications sent doesn't reach the device. Even if I start again the app the notification is lost and is never received.
I also tryed this:
close the app process -> shut down the device -> power on the device -> send a notification... and the notification is not received!
It seems that firebase can only receive notifications if the device has the app started and not 100% closed, I mean, closing it just with back key but not killing the app process.
How is this possible? It is supossed that firebase should receive notifications even with the app closed.
I'm testing on a Nexus 5X with Android 8.0 and I'm using the last version of Firebase push Notifications.
Sorry for the late, but hope this help next users that will have this problem because there is no answer selected as "Solution".
When setup correctly the service, this will work even the app is closed. That because, Firebase Messages travel by Google Play Services so closing your app doesn't have a relation with the service.
At first, notification never came. By searching in the device settings I saw that the energy saving system for my app was active (when closed was removed from stack) so notification was sent but my app couldn't take and display these.
After disabling that option, I've test many time and I found that sometimes notification come with a late of 2-3 minutes when app is completely closed. Sometimes it touch the 5 minutes. You need to be patient and it will come!
Instead, When app is opened or closed simply by back button, notification come in few seconds.
In your AndroidManifest.xml file remove android:exported=false from your Messaging service.
Explanation: When your app is completely killed or removed from back stack. OS tries to restart the messaging service but if there is android:exported=false in your manifest file then OS will not able to restart the service because such service can only be restarted by the same app.
Reference: https://developer.android.com/guide/topics/manifest/service-element#exported
It seems that firebase can only receive notifications if the device
has the app started and not 100% closed, I mean, closing it just with
back key but not killing the app process.
No, FCMs are sent to all the devices that have Google Play services and the targeted application. That is why it is called Push Notifications.
Your application also get notifications when it is running, to handle those you need to override
onMessageRecieved(RemoteMessage mes);
There could be many reasons for the app not getting notifications. Some of them could be :
Messaging Services not included in the Manifest
Play services not configured correctly. Or not present in the Phone.
Sometimes Latency is High (rarely). I noticed it sometimes take take about 2-3 minutes
after composing.
SHA1 fingerprint not registered in Console and/or updated google-services.json not present in sources.
Uninstall and reinstall the app. So that token Regeneration may take place.
Please follow this link to get started with messaging.
https://firebase.google.com/docs/cloud-messaging/android/client
Have you added firebase services on Java code?
Here is the link: firebase/quickstart-android
You have to add those 3 java file in java folder and also add those service name in AndroidManifest.xml
if you are sending it from your firebase console it sends a notification message so those you will not get if your app is closed, you need to send messages that have the data payload which the console does not do.
https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
Took this screenshot from Firebase documentation, seems its not possible with FCM
Related
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
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
}
}
I am testing the sample project for gcm notification from google
https://developers.google.com/cloud-messaging/android/start
This project is using com.google.android.gms:play-services-gcm:9.0.0
After I install the app to my phone via android studio.
I kill the app from my phone(not force kill from setting).
Then, I tried to send notification message according to google instruction.
Notification cannot show on my phone. When I check the logcat. I found the following log about GCM.
06-08 11:19:37.859 5080-5080/? W/GCM-DMM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=gcm.play.android.samples.com.gcmquickstart (has extras) }
After that I launch the app in phone and kill the app again, then send GCM again to my phone. At that time, notification can show correctly in my phone.
It seem, application need to restart one time for receiving gcm notification or application should be received notification before user kill the app.
How should I solve this problem?
As I know, lower version of play-services-gcm:9.0.0 do not have this kind of problem.
It's a feature of the Android platform. Force stopping an application by the user puts the application in a stopped state and none of its code is run, including any broadcast receivers declared in manifest. Only when the user explicitly launches the app it is put in a state where the receivers get fired.
Read this thread
EDIT: It maybe that you gym token doesn't have time to register with the server. Do you get a token and a registration confirmation from the server?
If your just starting with GCM then I would suggest to use FCM it is replacing GCM.
https://firebase.google.com/docs/cloud-messaging/
Same thing but with support and new examples. It looks even easier to use than GCM.
When I installed the app by using apk file, this problem is not happen. This problem is only happen when I install the app by running project in android studio. Thanks for all suggestion and answer.
Best fix I have so far is to unplug the phone, kill the app, restart the app then kill it again. Doing this will have the notification displayed even after installing it via android studio.
Here is another thread regarding the issue: https://code.google.com/p/android/issues/detail?id=219084
Pretty much the title. I have the app register the device to receive push notifications when it runs, I need to unregister the device when it uninstalls. How to?
I guess you write about uninstall in the meaning of deleting the app from the device?
Unregistering from GCM on the device-side may take several minutes and therefor probably can not be done on uninstall, see answers here.
Your server should just stop sending push notifications. If he tries GCM returns an NotRegistered-error, see the docs here.
If you're using Appcelerator services for notifications then the error comes from their servers.
I have used GCM to get push notifications, now if I Force stop the app from the settings on the Android device, will it be able to get push notifications?
I have read many posts that say in this case an app cannot receive notifications.
Is there any possibility to get notifications?
Once you force-stop your app from Settings, your code will not run until something manually runs one of your components (ie the user manually launches an activity).
Therefore after force-stopping your app from Settings, you will not receive GCM messages.
If you want to get notifications you have to manually restart your app.
This is by design since Android 3.1.
Apps that are in the stopped state do not receive broadcast Intents.
Stopped state is:
when the app is initially installed (before the user runs something in
the app) or
after a Force Stop.
You can find more about this here: http://developer.android.com/about/versions/android-3.1.html#launchcontrols