Reputation: 1012
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.
Upvotes: 5
Views: 6318
Reputation: 2100
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
Upvotes: 0
Reputation: 1512
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.
Upvotes: 0
Reputation: 1036
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.
Upvotes: 0
Reputation: 425
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.
Upvotes: 0
Reputation: 1662
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.
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
Upvotes: 3
Reputation: 777
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"/>
Upvotes: 5