NullPointerException
NullPointerException

Reputation: 37733

Firebase Push Notifications not received if app is closed

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 tried 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 supposed 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.

Upvotes: 17

Views: 32956

Answers (6)

pavithra ramasamy
pavithra ramasamy

Reputation: 9

Took this screenshot from Firebase documentation, seems its not possible with FCM enter image description here

Upvotes: -1

priwiljay
priwiljay

Reputation: 2743

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

Upvotes: 7

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.

Upvotes: 11

tyczj
tyczj

Reputation: 73996

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

Upvotes: 0

coder3101
coder3101

Reputation: 4165

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

Upvotes: 1

abir-cse
abir-cse

Reputation: 546

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

Upvotes: 0

Related Questions