firebase / quickstart-unity

Firebase Quickstart Samples for Unity
https://firebase.google.com/games
Apache License 2.0
828 stars 428 forks source link

FirebaseDatabase.DefaultInstance.GetReference(".info/connected") not working for Android build #1220

Closed rozatlabs closed 2 years ago

rozatlabs commented 2 years ago

[REQUIRED] Please fill in the following fields:

[REQUIRED] Please describe the issue here:

My game checks the connection to the realtime database as follows:

`. using UnityEngine; using Firebase.Database;

public class NoInternetPanel : MonoBehaviour { public GameObject noInternetPanel; DatabaseReference connectedRef; bool isConnected;

private void Start()
{
    connectedRef = FirebaseDatabase.DefaultInstance.GetReference(".info/connected");
    connectedRef.ValueChanged += CheckInternetConnection;
}
private void OnDestroy()
{
    connectedRef.ValueChanged -= CheckInternetConnection;
}

public void ActivateNoInternetPanel()
{
    noInternetPanel.SetActive(true);
}

public void CheckInternetConnection(object sender, ValueChangedEventArgs a)
{
    isConnected = (bool)a.Snapshot.Value;
    if(isConnected == true)
    {
        noInternetPanel.SetActive(false);
    }
}

}`

This has been working perfectly for my iOS release this past year, but now that I'm trying to release for Android, it is not working correctly. At almost exactly 1 minute after launching the game, isConnected returns false. Everywhere I look, this seems to be the way to check the connection, and like I mentioned it works on my iOS release of the game. Is there something specific to Android that needs to be done differently either in my code or in the Firebase setup? Completely stuck on this! Many thanks for any help

argzdev commented 2 years ago

Hi @rozatlabs, thanks for your report. We'll investigate this and see what we can find.

rozatlabs commented 2 years ago

Hi @rozatlabs, thanks for your report. We'll investigate this and see what we can find.

Thank you for looking into it, any update to this issue?

paulinon commented 2 years ago

Hi @rozatlabs,

I've tested your code on an Android build, and I haven't encountered the behavior you described. To be specific, isConnected was set to false or a second, and then it stayed true afterwards. Are there other steps that have to be taken into consideration when trying to replicate this issue?

rozatlabs commented 2 years ago

Hi @paulinon, thank you for looking into it. No other steps needed as far as I am aware of. At what point was isConnected set to false? I was experiencing the issue approx 1 min after app launch. Is there a reason it would be returning false on Android and not iOS?

paulinon commented 2 years ago

Hi @rozatlabs,

After the Android app was launched, isConnected is observed to be false for a second, and then it became true right after. There were no changes observed after a few minutes or so. I've tested this on an iOS build, and it shows the same behavior.

It's possible that the network connectivity is causing the issue. Could you try if changing the network shows any difference? Additionally, is there any data that is needed to be stored in the database before launching the app?

rozatlabs commented 2 years ago

Hi @rozatlabs,

After the Android app was launched, isConnected is observed to be false for a second, and then it became true right after. There were no changes observed after a few minutes or so. I've tested this on an iOS build, and it shows the same behavior.

It's possible that the network connectivity is causing the issue. Could you try if changing the network shows any difference? Additionally, is there any data that is needed to be stored in the database before launching the app?

Changing networks from wifi to mobile has same result, after 1 min isConnected returns false. No data needing to be stored before launching. At launch, my app checks database connection and compares timestamps of a user’s last login. That part is working correctly at launch so I know I’m getting a connection. But still after 1 min it returns false.

Just to be sure my listener was working properly, I launched the app, quickly put the device in airplane mode (causing isConnected to return false momentarily) then disengaged airplane mode and isConnected returned true again. So this means isConnected is continuously returning false after 1 minute, until I close the app and relaunch. Does that help narrow things down at all?

paulinon commented 2 years ago

Hi @rozatlabs,

Thanks for the additional information. I've also observed this behavior during testing.

Could you verify if this issue persists on other Android devices as well? If so, please provide debug logs from the Android Logcat from app launch to the moment isConnected becomes false so that we can analyze what's causing this.

google-oss-bot commented 2 years ago

Hey @rozatlabs. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

rozatlabs commented 2 years ago

My apologies for the delay, I've been trying to track down things on my end as much as possible so as not to waste anyone's time/efforts here, however I'm still unable to track down the issue. I unfortunately only have a Samsung Galaxy A32 for testing, but here is the most recent logcat. In Update(), I printed the status of isConnected so you can see when it changes (apologies for this causing it to be quite verbose)

When I create a new player profile, it successfully creates a new auth account, as well as realtime database nodes like the iOS release, so the connection is working properly at that point. I'm stumped as to why it's returning false seemingly randomly. Thanks for your help tracking this down, I hope the logcat helps! Android logcat 01.31.odt

paulinon commented 2 years ago

Thanks for this, @rozatlabs. Version 8.8.0 of the SDK has been released recently. Could you try this version and see if this fixes your issue? If it doesn't, please provide a new set of debug logs so we can analyze what's causing this.

google-oss-bot commented 2 years ago

Hey @rozatlabs. We need more information to resolve this issue but there hasn't been an update in 5 weekdays. I'm marking the issue as stale and if there are no new updates in the next 5 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

rozatlabs commented 2 years ago

The updated SDK seems to have solved the connection issue! Much appreciated

paulinon commented 2 years ago

Glad to hear this, @rozatlabs. That being said, I'll be closing this for now.