I'm not sure if this is a bug or not. If you'd rather I close this and discuss on Discord I can, but there are so many simultaneous discussions on Discord that sometimes it is hard to get answers.
All Firebase modules are working flawlessly when app is in the foreground, and for some time while in the background. After some time passes, we have trouble logging events and getting user auth tokens.
For example, we're having trouble with both of these:
Firebase.auth().currentUser.getIdToken(true).then((token) => {
doSomethingWithToken(token);
});
Firebase.analytics().logEvent('some_event', {
parameter1: 1,
parameter2: 'B'
});
After some period of time we can't seem to get any new tokens, so our app starts to send expired tokens with it's requests to Cloud Functions, resulting in 403 errors. When we can't get any new tokens, the analytics events also stop logging.
For example, we sent roughly 230,000 HTTP requests to Cloud Functions with auth tokens attached in the past 30 days. Roughly 55,000 of those failed from expired tokens. We log an analytic event on the client every time a request is sent, and we only received roughly 175,000 analytic events. Call me crazy but thats one hell of a coincidence.
Any ideas? Thanks.
@wbattel4607 Can I just clarify, when you say that it works for some time in the background, does that mean that the app remains in the background when these issues are happening? Or is the app in the foreground when the issues start?
From what I can tell, and I am not 100% sure, the issues are starting some time _after_ the app enters and remains in the background (but not terminated/suspended). They don't seem to happen immediately- nor predictably for that matter. But it seems to occur eventually when the app is in the background state.
If the app comes back into the foreground, does it start working again?
We think that is the case. Due to its unpredictability, we're having difficulty troubleshooting this to confirm exactly when the problems start and stop. We're not seeing any issues when the app is in the foreground, including when returning from the background where these issues had occurred.
I'm thinking this _could_ be related to my previous issue with AdMob (#442). In that issue I had noticed AdMob had to be re-initialized every time the app returned from the background. Perhaps other Firebase modules are de-initializing when he app moves to the background like AdMob does? Except, unlike AdMob, these modules are functioning correctly on returning to the foreground.
Just food for thought. I have no idea what's going on. 馃檭
It certainly sounds like in the background the app is somehow being restricted.
Out of interest, what exactly are you trying to do whilst the app is in the background?
Our app is location-based, so we have to update the user's location when they move. For 90% of those location updates, the app is in the background.
Every time the app registers a location update, it logs an Analytic event and sends an HTTP POST request to Cloud Functions with the location information and user auth token for our system to update the database, send notifications, etc....
After each HTTP request, we forceRefresh an auth token from Firebase and update the URL and HTTP Headers for the next request.
The HTTP requests are coming through no problem, but Firebase fails to keep the user auth token up to date and log the analytic events.
We're also running into another problem with authentication that may or may not be related in terms of the cause. I'm going to open a separate issue for it because it's a separate problem.
A very random shot in the dark but this question on SO seems slightly similar and am wondering if the solution in the first answer holds any bias, no harm trying it: https://stackoverflow.com/questions/41177624/firebase-unity-sdk-100-hang-on-ios-when-the-application-resumes - the info plist thing
On our end though I'm not even sure where we could begin to debug this, seems hard to consistantly reproduce, I'll have a look through the auth/analytics code tomorrow and see if anything sparks an idea.
@Salakar I'll give that a try but, like you said, it's so inconsistent that it'll be hard to tell if it helps unless we ship it in our next production version. We can do that no problem, but it will take some time to figure out if it makes a difference.
Given that the SO question is regarding resumed app sessions, as opposed to our case with sessions running in the background, I doubt it'll make any difference. No harm in trying.
@wbattel4607 are you triggering the background logic with a silent push? If so, iOS may be arbitrarily suspending your thread before you can get your work done, as I've detailed in #971.
Nope, the app is running normally in the background. And this has been a problem for us since the beginning when we used version 2.1.3 of this library. We're currently on 3.3.1, with plans to upgrade to 4.0.4 in the near future.
That being said, I do think the problem could be that iOS is suspending _something_, but not the whole app since there are parts we've observed to run indefinitely.
Even if the user manually terminates the app (double-home, swipe up on app), it will reboot in the background and run normally until eventually hitting the same hiccup.
Our app's location system has no problem running for an infinite period of time, we're only having trouble keeping Firebase alive.
This issue has been automatically marked as stale because it has not had recent user activity. It will be closed if no further activity occurs. Thank you for your contributions.
Closing this issue after a prolonged period of inactivity. If this is still present in the latest release, please feel free to create a new issue with up-to-date information.
I am noticing this as well very intermittently. It seems at random times when you move the app to the background and then to the foreground - Usually after just a few minutes in the background the connection to firebase seems to break and you stop receiving any data. You have to kill app and restart to fix. Is there something we can use with AppState to reinitialize connection etc?
I am noticing this as well very intermittently. It seems at random times when you move the app to the background and then to the foreground - Usually after just a few minutes in the background the connection to firebase seems to break and you stop receiving any data. You have to kill app and restart to fix. Is there something we can use with
AppStateto reinitialize connection etc?
One year later, this issue still happens (mainly on android)
Have you been able to come up with a solution @srhise ?
Leaving the app in background, using any other app for just a few seconds, then back to the app in foreground: firebase takes around 30seconds to resync; sometimes it never does and killing the app is the only solution.
This is all so random, makes it impossible to fully understand.
Any help would be appreciated
This seems like it would be an upstream bug in the SDKs, I'd be surprised if it was something react-native-firebase was doing (though I am frequently surprised). Can it be reproduced with a sample project using nothing but the native SDKs? Is there anything in the google firebase sdk repo about it? And are you using the most recent firebase sdks (ios is 6.13.0 and android BoM is 24.2.0 at time of this writing)
Most helpful comment
I am noticing this as well very intermittently. It seems at random times when you move the app to the background and then to the foreground - Usually after just a few minutes in the background the connection to firebase seems to break and you stop receiving any data. You have to kill app and restart to fix. Is there something we can use with
AppStateto reinitialize connection etc?