5

I am new to android and I was able to setup the firebase for my app in kotlin. If i run the app in the Nexus 5X API 27 emulator I am able to get the database but when i run the app in the actual device SAMSUNG S5 (Google play Services V 12.5.29, android V 5.0) i don't get the addValueEventListener call back.

ref = FirebaseDatabase.getInstance().reference
    ref!!.addValueEventListener(object : ValueEventListener {
        override fun onCancelled(p0: DatabaseError?) {
            Log.d("firebase", "cancelled")

        }

        override fun onDataChange(p0: DataSnapshot?) {

            if (p0!!.exists()){
                Log.d("firebase", "date = $p0")
            } else {
                Log.d("firebase", "no data")
            }

        }

    })

Gradle file:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'


android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.wonder"
        minSdkVersion 21
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner 
        "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 
    'proguard-rules.pro'
        }
    }
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.google.firebase:firebase-messaging:11.2.2'
implementation 'com.google.firebase:firebase-database:11.2.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso- 
core:3.0.1'

//implementation 'com.google.android.gms:play-services-maps:11.2.2'
implementation 'com.android.support:recyclerview-v7:26.1.0'

implementation 'com.google.firebase:firebase-core:11.2.2'
//    implementation 'com.google.android.gms:play-services-ads:12.0.0'
}
apply plugin: 'com.google.gms.google-services'

I know its not database rule issue because they are set to public.

If you have any suggestions please let me know.

Thanks

EDIT: I do have permissions added in the manifest file

Update: Looks like its working for the device, I just have to wait for 30 minutes. I left the device and came back after a while and the data was there. Could it be threading issue? I am not doing any threading, thought Firebase handles it by itself.

Paragon
  • 982
  • 2
  • 10
  • 37
  • please post your complete `build.gradle` code – hakim May 08 '18 at 01:43
  • @hakim added, let me know if thats enough – Paragon May 08 '18 at 01:47
  • do you obfuscate the source code of your app when build apk for your device? – hakim May 08 '18 at 01:49
  • not sure what do you mean by that? – Paragon May 08 '18 at 02:05
  • please post all build.gradle source code not just the dependencies part, there should be other configuration. I want to check if there is misconfiguration (ie. your forgot to update your proguard configuration) – hakim May 08 '18 at 02:10
  • done - wasn't sure if that was needed – Paragon May 08 '18 at 02:15
  • Just to understand you are getting data initially but after 30 mins when you come back to screen again data goes off.. is it the problem here ?/ – santosh kumar May 11 '18 at 15:05
  • @santoshkumar no I don't get the data initially. I got the data after ~30 minutes. I tried couple of times today again but wasn't able to get the data at all. It worked well on the emulator – Paragon May 11 '18 at 18:39
  • 1
    I was having a similar issue that resolved itself by going off wifi. Really not sure why or whats up there. – NewWorldTom Jan 25 '19 at 02:26
  • Does your real device have a sim card inserted? I noticed that it's not working on my device without sim card but on my other device with sim card its working fine. Also on the emulator its working fine. – Simon Schubert Aug 27 '19 at 11:28

6 Answers6

5

Make sure in your Manifest file for the app (the Main manifest file) that you have:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.INTERNET"/>

Kwright02
  • 777
  • 3
  • 21
3

It may happen if you are using release build on the device. Make sure you have added your release sha1 in your firebase app console.

enter image description here

Click on Add Fingerprint to enter your release sha1.

In your emulator maybe it is working because you have added your debug sha1 and package name in your firebase app key.

Check this for generating sha1 for release build

Rizwan
  • 1,461
  • 12
  • 26
0

You app might not compatible with that device. Check your supported min version and settings to make your app compatible with this device too.

(or)

Check Callback:

Callbacks are removed by calling the removeEventListener() method on your Firebase database reference.

"If a listener has been added multiple times to a data location, it is called multiple times for each event, and you must detach it the same number of times to remove it completely. Calling removeEventListener() on a parent listener does not automatically remove listeners registered on its child nodes; removeEventListener() must also be called on any child listeners to remove the callback."

(or)

If you have some special characters in class name or some file name, please rename it because some devices greys out invalid characters.

kavita
  • 417
  • 4
  • 8
0

From here

Try calling, FirebaseDatabase.getInstance().setPersistenceEnabled(true); inside your Application class's "onCreate" method.

I think your problem might be the connection. Enabling the persistence listeners callbacks will continue to fire for local updates

You can read more about it here.

TOvidiu
  • 1,036
  • 11
  • 15
0

I had similar problem it got me crazy. The app was working well and suddenly i stopped getting data from firestore but when i added network state permission it worked

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

don't know the reason but this permission saved me.

Islam Assem
  • 1,376
  • 13
  • 21
0

In my case, I just had to reset my WiFi router. I don't understand why but it worked, this answer is what helped me: Firebase database listeners don't work on android with wifi

Adam Zarn
  • 1,868
  • 1
  • 16
  • 42