16

I am developing an e-mail app in which I want that the user will get a push notification as soon as they receive new email. And for that purpose I am using FCM. I have just tried push notifications using FCM by following this link: https://www.youtube.com/watch?v=XijS62iP1Xo&t=6s in order to test what features FCM provides. But the problem I face is that the device receives push notifications when app is either in foreground or background but it won't receive any push notifications when the app is closed (swipe or clear from the task manager). I don't know how to achieve this via FCM? I want to receive push notifications just like the WhatsApp and Facebook apps.

Every kind of help is appreciated. Thanks in advance.

JJD
  • 50,076
  • 60
  • 203
  • 339
Iqraa
  • 385
  • 1
  • 6
  • 18
  • Possible duplicate of [How to handle notification when app in background in Firebase](https://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase) – Andrey Danilov Nov 20 '17 at 18:35
  • I know this may seem like a duplicate question but I have tried the other methods but I still cannot seem to get it. – Iqraa Nov 20 '17 at 18:36
  • if your app is closed (not force killed) you will get messages in the service when you use Data messages. If your app is force killed then you wont get them depending on the device/OS version/Manufacturer. See this answer https://stackoverflow.com/questions/39504805/android-app-not-receiving-firebase-notification-when-app-is-stopped-from-multi-t – tyczj Nov 20 '17 at 19:30

4 Answers4

14

There are 2 types of push notifications: Data messages and Notification messages.

If you are using the Data messages you will be in charge of handling the received message and present a notification to the user (if needed of course). But in this case you might miss notifications when your app is closed.

If you are using Notification Messages, FCM is handling the message for you and directly displays a notification if the app is in background/closed.

Please see more here.

Cata
  • 11,133
  • 11
  • 65
  • 86
  • 5
    You might miss notifications when your app is closed! seriously?? – Xenolion Nov 20 '17 at 19:05
  • How does that happen I have never seen that?? – Xenolion Nov 20 '17 at 19:11
  • 1
    @Xenolion : check in 8.0 devices specially Xiomi, Oppo, Vivo, Lenovo. They don't shows up notification, but if you turn off battery optimization notification will shows up. but problem is we can't go to user and tell to do it. – Sagar Panwala Jul 25 '18 at 03:33
  • Is it related to battery optimization OR is it because of not using notification channels? But I think that's not good, they should surely reconsider the design or at least using AI how the user interacts with apps. Because he might miss some notifications in an App like a chatting app! – Xenolion Jul 25 '18 at 18:10
  • 1
    I turned off battery optimisation for my app, and now when I swipe out the app I receive the notification! – d51 Nov 23 '20 at 00:16
  • can you please take a look at this question https://stackoverflow.com/questions/76225714/unable-to-receive-notification-in-battery-optimization-mode-pushy – Qasim Malik May 18 '23 at 07:45
10

It is not possible to receive a push notification in an app that has been killed/stopped by "Force stop" in application settings:

Force stop in application settings

Let me quote the response I got from Firebase Support when I posed them that question:

The Android framework advises that apps that have been stopped (i.e. killed/force-stopped from Settings) should not be started without explicit user interaction. FCM follows this recommendation, and thus does not deliver messages to stopped apps. Here are some documentation that discuss this topic:

This confirms what I observed when I tested it using a simple app.


But you should be able to get push messages from FCM when the app is in background, even if it was swiped off from Recents screen, or after system reboot. That is, unless the manufacturer made the swipe gesture to work the way "Force stop" does on your device. How you receive it in the background depends on whether the push notification contains the "notification" payload:

  • If it does, you will receive the "data" only if user taps on the notification and it will be delivered in the Intent extras to the activity launched by the notification action.
  • If it doesn't, you will receive the notification in onMessageReceived just like you do when the app is in foreground.

Some other cases when your app is not killed, but still may not receive push notifications:

arekolek
  • 9,128
  • 3
  • 58
  • 79
1

If your App is Killed or in background,check for the Payload in your Launching Screen in My case it is MainActivity so in onCreate() Check for Extras:

if (getIntent().getExtras() != null) {
    for (String key : getIntent().getExtras().keySet()) {
        Object value = getIntent().getExtras().get(key);
        Log.d("MainActivity: ", "Key: " + key + " Value: " + value);
    }
}
Gaurav Lambole
  • 273
  • 3
  • 3
0

Yes only if you consider sending data payloads not notifications and handle it in onMessage()

get more info here

How to handle firebase notification on background as well as foreground?

vikas kumar
  • 10,447
  • 2
  • 46
  • 52