Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase android works on the emulator but not on the device

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.

like image 498
Paragon Avatar asked May 05 '18 23:05

Paragon


1 Answers

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"/>

like image 135
Kwright02 Avatar answered Sep 30 '22 17:09

Kwright02