Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can Firebase console push notification if apps is closed? (User swipe away with the long click menu) #41

Closed
jamesytl opened this issue Jun 7, 2016 · 103 comments

Comments

@jamesytl
Copy link

jamesytl commented Jun 7, 2016

I have tried developing a firebase notification apps with this MyFirebaseMessagingService link and MyFirebaseInstanceIDService link example that will receive notification from the console. However, if I close the apps by swiping away the app from the task manager, the app will no longer be able to receive any notification.

I have tried using a service that return START_STICKY and using it to start the MyFirebaseMessagingService but still couldn't receive any notification.

Is this how the firebase push notification intended to perform or am I doing it wrongly?

Extra Info. : My apps is working if I only click back but not if I swipe it away in task manager.

@fockeRR
Copy link

fockeRR commented Jun 15, 2016

Same problem here. I created simple app using the exampe app from github. When the app is foreground or background everything works as expected and described in the docs. But, when I killed the app, I can't received the push notifications ;<

@yishai-glide
Copy link

@jamesytl and @fockeRR are you seeing logs in the onMessageReceived?

@fockeRR
Copy link

fockeRR commented Jun 15, 2016

@yishai-glide No, it looks like the onMessageReceived is not called. If the app is in background or foreground I see the logcat output from onMessageReceived

@jamesytl
Copy link
Author

@fockeRR @yishai-glide It seems like it depends on device being tested. When I use Oppo phone the apps will no longer receive notification when the app is "swiped off". However, RedMi Note 3 will still receive notification if I perform the same action.
So probably could say that its android phones with different OS messing with us and causing headaches

@harshil007
Copy link

@jamesytl I am using RedMi Note 3 and facing the same problem. The app can receive notifications while running in foreground or background, but won't receive notifications when the app is killed. The same application is working fine on Nexus 5. Also I have another application which uses GCM implementation to receive notifications and it works fine on both devices even when the application is killed.

@kroikie
Copy link
Contributor

kroikie commented Jun 23, 2016

@harshil007 With your working GCM implementation are you also sending messages from the Firebase console (notification messages) or are you using data only messages?

@jamesytl
Copy link
Author

jamesytl commented Jun 24, 2016

Update : Tested using 2 RedMi Note 3 with different ROM.

One worked perfectly as how I wanted( Able to receive notification even if swiped off, immediately receive notification after boot with broadcastreceiver)

while another phone failed miserably ( No longer receive any notification after swipe off, will never receive any notifications after reboot even if I manually start the program after boot. Re-installation of the app will instantly receive previous notifications but this situation is totally wrong because new installation should generate new registration token. Its like the notification has reached the phone but never triggered the FirebaseMessagingService to display the notification).

@harshil007
Copy link

@kroikie Data messages only as it is a google project which I have not yet imported into the firebase console.

@harshil007
Copy link

UPDATE : App working perfectly now in RedMi Note 3. It turns out you have to give auto start permission to the application in order to be started up after being killed.

@schonmann
Copy link

schonmann commented Jun 28, 2016

I have a similar issue. My problem is basically that i need to receive every notification in my implementation of FirebaseMessagingService (onMessageReceived), even if app is in background or closed/killed (like messaging application services, for instance). The scenario is:

  • When my app is swiped off i can only get system tray notifications, as expected.
  • When i kill my application, i can't receive any notification.
  • Weirdly, when i shutdown my cellphone, push as many notifications as i can, wake it up and open my app, i still receive all of the push notifications (Why not when app is killed?).

What i've tried:

  • Create a START_STICKY service (extending 'Service' class) running in a different process to "wake-up" my FCM service, but that approach is not working. I do receive the FCM message intent, onStartCommand method get's triggered, but i can't start up my FirebaseMessagingService (application process is dead). The notification and all it's important data are now lost. If the app is in foreground, it works at least.

Any advice will be appreciated!
Thanks!

@jamesytl
Copy link
Author

jamesytl commented Jun 28, 2016

@schonmann by killing apps you mean force close right? Force close will close down an app entirely including any background services even if START_STICKY is included if I'm not mistaken.

Btw, I just learnt in last few day we should not use a service with START_STICKY to start the FCM service because FCM service is an IntentService which will automatically start its own when needed and stop itself when finished.

Not sure its relevant or not but I'm now using data payload instead of notification payload and handle the data's included in notification personally. This way the notification received will always work as how we wanted it to work instead of default FCM way of handling notification which will show notification at system tray with notification payload's head and body. This may fixed the issue of your app receiving notification only when in foreground.

You may check this link for more information.

@schonmann
Copy link

@jamesytl I think my problem is finally solved. I was sending push messages with both data payload and notification payload, so the system tray was handling it.. hahah
When i get home i will try that. Thanks!

@kroikie kroikie closed this as completed Jun 28, 2016
@jamesytl
Copy link
Author

jamesytl commented Jun 29, 2016

@schonmann yup that's what I did previously too. Try sending data payload only and remember to handle it in your FirebaseMessagingService's OnMessageReceived(). Good luck coding ;)

PS: My RedMi Note 3 still have some issues like not receiving any notification until it is manually disconnect and reconnect to a Wi-Fi connection. Not sure how it works but I guess its still device dependent as the two same devices will yield different result.

@chandrahasan
Copy link

chandrahasan commented Jun 30, 2016

@jamesytl , I can get notifications when App is Foreground/Background , But when I swiped from recent tray then Firebase Notification not coming . Any possible way to handle this ?
I check same scenario in whatsup , After swiped from recent tray Whatsup Notification still coming ,even I force stop whatsup from settings but still i can get whatsup Notification.

I must want to handle notification when app is closed (wiped from recent tray) . Any possible ways please advise.

I tried , Samsung,huawei,Mi devices.

$path_to_firebase_cm = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
    'to' => 'fVRFdJKlgX4:APA91bG7AzLsrux5RQCeRo1ykddr_Ygl4_FAQpKYPUleVkC7NgZMJy-Wjn0smMP8H6bimfuakFGN3ZMP2UYej8tP40zOJ-RIGZhYpk1DYySifDQ0BrZUmBkKEy1dNWLgj2H84sdssTTIhNf',
    'data' => array('title' => 'Working  Good', 'body' => 'That is all we want','message' => 'message'),
);

Thanks in advance.

@jamesytl
Copy link
Author

jamesytl commented Jul 2, 2016

@chandrahasan Sorry for late reply and also sorry that I can't really help you because I'm not sure why yours isn't working. Just some common question when it comes to FCM notification.

  • Have you remove your notification getbody method at FirebaseMessagingService?
  • Did you handle the data at OnMessageReceived() method?
  • Do your devices have security apps? If yes did you allow auto start for your apps? harshil007 at above mentioned that some device's security apps block the intentService( FirebaseMessagingService in this case) from starting if auto start not allowed.

By the way, can't really compare normal apps with whatsapp because it seems like it binds itself with our phone's Contact which is never closed?(at least that's what I found from little information gathered through google regarding how whatsapp's notification works)

@rio45ka
Copy link

rio45ka commented Aug 11, 2016

@jamesytl same problem with @chandrahasan. But for me if app dead because killed automatically by ActivityManager. Notifications not received.
I'm using Asus Zenfone with 5.0 Lolipop.

@kroikie
Copy link
Contributor

kroikie commented Aug 11, 2016

@chandrahasan and @jamesytl The Android framework advises that apps that have been stopped (not in background) should not be started without explicit user interaction. FCM follows this recommendation and thus does not deliver messages to stopped apps. What I believe is happening on the devices in question is that apps that are swiped from the recents list are being stopped and thus not receiving messages from FCM.

Note that some images may whitelist applications originally included with the image. So I'm assuming that in the case of WhatsApp it may have been whitelisted by the system and thus is not seen to be stopped at message delivery time. Swiping from the recents list should not result in applications being stopped, it should only stop the Activity.

I will take a closer look at these devices and report back here when I have more.

@tariqul000
Copy link

I send User segment notification but All user don't get push notification Why all did not get? From console.firebase.google.com I found monthly active user 4100 but when i send notification It shows 2100 send. That mean 50% user found. then what would be be others.

@jamesytl
Copy link
Author

@tariqul000 you should create a new issue as this seems irrelevant with this issue

@18ulhaspatil
Copy link

Is there any solution come on that issue or not ? cause i also have same issue, I receive Notification when my Mobile Screen is ON whatever the app is open or not, i got instant notification, but if i lock my Mobile more than 1 min i didn't able to receive any notification. Any Progress in that in FCM ?

@a100rabh
Copy link

I am also facing same issue with my Lenovo k5 plus.....
I Just go to settings/apps/
In this there are two check boxes ...

  1. for Show notifications.
  2. for Restrict to launch.

both option are by default selected...I unselect the second option
and now my application is receiving notification all the time.

Is anyone know how to do it with programming
screenshot_2016-10-19-16-19-55

@humzakhalid94
Copy link

Can anyone tell me the final conclusion of what happens if application is killed?
I'm not asking if app is in background

@radityagumay
Copy link

@ALL this issues are restricted by Android Devices. Several device has restrictions like Azus Zenphone. Installed new app will have recommendation to allow or disallow power management. But, app like wa, line, bbm they can force allow auto manager. Anyone can solve this issue?

@RaniRashmi
Copy link

RaniRashmi commented Nov 18, 2016

Hi
I have downloaded and installed quickstart messaging project.I am getting Notification message first time(when I am running application) from Firebase cosole without calling sendNotification() method which I have created in MyFirebaseMessagingService.Can anyone Please why this is sending Notification message to my mobile for the first time without calling sendNotification() method.

@owaremx
Copy link

owaremx commented Jan 12, 2017

@kroikie that's true, I have a huawei phone (with EMUI) and I had to open the apps settings, then select my app, then battery and then play with battery tweaks, so, the operating system don't kill my app when I turned off the screen or when my app was idle by a long time. Thanks man

@Sahana312
Copy link

Is there any work around for this? I see inshorts, Times now and TOI sending notifications when the app is killed

@tinyCoder32
Copy link

I have the same issue on Lenovo A6020a41 even if I removed the restrict to launch option, I still can't get the notification when the app is killed from the recent apps list.

@kroikie
Copy link
Contributor

kroikie commented Jan 29, 2017

Hi @tinyCoder32 could you check the logcat and see if your app is being stopped when it is removed from the recent apps list. We have seen some devices do this and once an app is stopped FCM will not wake it to send it a message till the user reopens the app.

@skfaisal93
Copy link

I found so many apps which are new on PlayStore but still I receive the push notification on my OnePlus 5 (as they are new on PlayStore hence they are not whitelisted from manufacturer company like WhatsApp & Facebook). But sadly for my app I am still unable to figure out how can I solve this issue with my OnePlus 5.

@SagarPanwala
Copy link

@skfaisal93 : agree with you, many apps are getting notifications which I think might not be whitelisted or not that much popular like fb and whatsapp, but still getting notification.

@ibrahimAlii
Copy link

I have this Issue too, when app is closed I didn't receive any notification, I'm tried to make a JobScheduler to schedule and check for notification every some time, but I didn't get a good result.

@Ktirumalsetty
Copy link

Does anyone found solution for this I have observed same with One Plus with 8.0 (Oreo)

@Haidar-Hammoud
Copy link

Is there really no solution yet? Does anybody have any way of ensuring these services run after app is swiped?

@Haidar-Hammoud
Copy link

Haidar-Hammoud commented Mar 5, 2018

I've found a solution!!!!
in the manifest, define this:

        <service
            android:name=".BService"
            android:enabled="true"
            android:stopWithTask="false" />

Then create the following java class:

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class BService extends Service {
    public class LocalBinder extends Binder {
        BService getService() {
            return BService.this;
        }
    }
    @Override
    public void onCreate() {
        for(int x = 0; x < 100; x++){
            Toast.makeText(this, "Wow", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("LocalService", "Received start id " + startId + ": " + intent);
        return START_STICKY;
    }
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }
    private final IBinder mBinder = new LocalBinder();
}

As you can see, there is a toast constantly being displayed. Notifications are indeed received and displayed, at least for me, even after the app is swiped away. If somebody can now play with this and find out something more practical than toasting infinitely in the onCreate method, maybe we can finally have a working solution.

What I've found is that it must be some type of loop, one log doesn't suffice. I've also discovered that this only works for about a minute. However, and most importantly, it works after the app is swiped away, so that's at least more progress than anything I've encountered.

@darklow
Copy link

darklow commented Mar 28, 2018

I have ASUS ZenPad 3S 10 (Asus P027) tablet with Android 7.0 and none of the solutions mentioned here worked for me, until I discovered app under settings that comes with tablet that blocks all background activities.
Under: Settings > Power management > Auto-start Manager I needed to turn checkbox ON for my app in order to allow background activities like push notifications. By default all newly installed apps were OFF, unless they came already pre-installed like Facebook, and other big apps.

@IgniteCoders
Copy link

IgniteCoders commented May 8, 2018

Yes, FirebaseMessagingService can receive PushNotification even when app is 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 you 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 the 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

@Mina-Mikhail
Copy link

Mina-Mikhail commented May 31, 2018

@IgniteCoders
I read your comment, it's so clearly. thank you.
but i want to handle a notification count to add it in app badge, i handled it in onMessageReceived() , but it working only when app is in foreground, you wrote in case to handle it when app in background(killed), you should use Firebase JobDispatcher..
I don't know how to handle it with Firebase JobDispatcher, can you help me ??!

@IgniteCoders
Copy link

Hi @Mina-Mikhail

I am glad to be helpful!

I dont have the code right now, but. The way to use JobDispacher is firing a Job when you receive the notification instead of trying to handle it in that moment. JobDispacher makes Job to be fired when there are resources for it (memory, baterry...).

I will post the code when i am at home, but there are lot of tutorials and a documentation in the link. Let me know if did it!

@kroikie
Copy link
Contributor

kroikie commented Jun 8, 2018

Hi I'd like to clarify that FCM will NOT deliver messages to apps that are in a stopped state.

From the Android documentation:

Applications are in a stopped state when they are first installed but are not yet launched and when they are manually stopped by the user (in Manage Applications).

Unfortunately some manufactures are incorrectly putting apps into a stopped state. Eg: when swiped from the recents view. Thus FCM will NOT deliver messages to applications in this state. We are working with manufactures to ensure that apps are only put into the stopped state as described in the documentation.

Backgrounded apps (Eg: apps that are swiped from recents or if the user leaves an app by going to the home screen.) do receive messages from FCM.

@gagangoku
Copy link

This is a major issue for me. I ended up not using flutter and developing my Android app the typical way.
On my OnePlus 5T, a normal android app (https://play.google.com/store/apps/details?id=com.homedruid.notificationtester) is able to receive notifications when swipe closed. However, when I create a flutter project, I don't get firebase notifications when app is swiped off.

I am sure this is not a whitelisting issue, my simple android app is new and is receiving notifications fine. I think the issue is somewhere the flutter sdk when onDestroy() is called.

I can't use flutter now (I really liked the ease of drawing widgets) for my customer app, push notifications is a MUST.

@gagangoku
Copy link

UPDATE: This might be a good news for OnePlus 5T. Can't say for other devices.
It seems if you have the word notificationtest in the package name, fcm notifications are received upon app swipe.

@Haidar-Hammoud
Copy link

I have some great news. I updated my firebase versions within Gradle and data notifications are working without fail now! It would seem that the firebase team fixed the problem.

@BestDI
Copy link

BestDI commented Jul 26, 2018

@HAIDARCORP Could you tell the fcm-messaging version you used?

@BestDI
Copy link

BestDI commented Jul 27, 2018

I have find that if the phone battery optimization is on, the push notification wouldn't be received after the app is closed.

@gerryhigh
Copy link

@gagangoku that seems to make no sense that it would work. From here https://github.com/flutter/plugins/tree/master/packages/firebase_messaging it shouldn't be supported

@gagangoku
Copy link

@gerryhigh I'm not using the firebase plugin, am directly registering with firebase using native android. I guess OnePlus 5T isn't terminating the application when app is swipe closed. With my old package name, it was and I wasn't receiving firebase notifications.

@dhruvishah22
Copy link

@Sanjay-Ghadge 's solution worked for me. Thanks!

What worked for you?

@erlangparasu
Copy link

erlangparasu commented Feb 28, 2019

  1. Your app: use AlarmManager to broadcast -> then register an implisit BroadcastReceiver (inside AndroidManifest.xml) -> then the receiver start an foreground service (with notification) -> then in that foreground Service, return START_STICKY.

  2. Firebase: send message (data) only.

  3. Android Settings: enable Autostart of your app.

@ricardoroth
Copy link

@kroikie after testing on my Huawei P20 it looks like the definition of "stopped state" is not complete. If my app is running (with FCM correctly displaying notifications in background state) and I reset my phone, then FCM doesn't display notifications anymore until I start the app again.
However... If I open the recent apps just after the reset, it does show up there!

Reseting the phone is not included as a cause for apps to stop on google manual.

Applications are in a stopped state when they are first installed but are not yet launched and when they are manually stopped by the user (in Manage Applications).

I really doubt that the intention here is that FCM will stop showing notifications of apps not restarted after a phone reset.

@thelilla
Copy link

TEAM- Is it still true that an app must be running in the background in order to get notified of the app's push notes?

@DennisSamual
Copy link

TEAM- Is it still true that an app must be running in the background in order to get notified of the app's push notes?

Yes. I tried solving this issue for the whole day (03/02/2020). And still I'am facing this issue. If anyone have a solution, please let us know.

Thank you in advance.

@Jacky-na
Copy link

Jacky-na commented Feb 3, 2020

#41 - ``

@frozenade
Copy link

I have some great news. I updated my firebase versions within Gradle and data notifications are working without fail now! It would seem that the firebase team fixed the problem.

Hello, would you give an example? It would be nice.

@zhuozp
Copy link

zhuozp commented Jan 7, 2021

@chandrahasan and @jamesytl The Android framework advises that apps that have been stopped (not in background) should not be started without explicit user interaction. FCM follows this recommendation and thus does not deliver messages to stopped apps. What I believe is happening on the devices in question is that apps that are swiped from the recents list are being stopped and thus not receiving messages from FCM.

Note that some images may whitelist applications originally included with the image. So I'm assuming that in the case of WhatsApp it may have been whitelisted by the system and thus is not seen to be stopped at message delivery time. Swiping from the recents list should not result in applications being stopped, it should only stop the Activity.

I will take a closer look at these devices and report back here when I have more.

Can you make sure that some app like skype, whatsapp are in whitelist. In fact, I had a test on skype. when have a call with skype, the skype can receive incall. But when I delete google play service, skype can't receive any incall when skype is closed.

@TiavinaRakotobe
Copy link

#41

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests