1

I have this application that receive push notifications,

first in my Application class i enabled disk Persistence like this :

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

then i receive push notification and open the application by clicking on it, here is the method that get invoked :

private void startIntent(final String msgID) {


    mRefMessages = fbdb.getReference(AppConstants.TableMessages.Table_name);
    mRefMessages.keepSynced(true);
    mQueryMessg = mRefMessages.child(msgID).orderByValue() ;

    Log.d(TAG  ,"ref=>\n" + mQueryMessg.getRef().toString() ) ;
    mQueryMessg.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            Log.d(TAG, "data snap Shot = " + dataSnapshot.getValue());


            if (dataSnapshot.hasChild(AppConstants.Key_Type))
            {
                Toast.makeText(ActivityMessages.this, "have child type == true" , Toast.LENGTH_SHORT).show();
                //open message view
                //some code . . .
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

            Log.d(TAG, "data base eror " + databaseError.toString());
        }

    });
}
  • the msgID is received in the intent extras,
  • the first time i open the application from the push notification, i print the data snap shot and the value exists and the other times the data snapshot has value dos not exists (data snap Shot = null ) !!
  • note that the data is exist in the fire base and the msgID is Correct , and the fire base reference is correct too.

if i go back and disable the Disk Persistence i dont have this behavior and the data snapshot value is found !

why this is happening ? what im missing ?

build.gradle snippet

dependencies {
    classpath 'com.android.tools.build:gradle:2.3.2'
    classpath 'com.google.gms:google-services:3.1.0'
    classpath 'com.google.firebase:firebase-plugins:1.1.0'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

and I am using 11.0.2 version of firebase sdk

android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
    applicationId "co.newandroiddummy"
    minSdkVersion 19
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    multiDexEnabled true


}
    compile 'com.google.firebase:firebase-database:11.0.2'
danny
  • 147
  • 1
  • 15
  • Using disk persistence and `addListenerForSingleValueEvent` don't mix very well. See my answer here for an explanation of the behavior: https://stackoverflow.com/questions/34486417/firebase-offline-capabilities-and-addlistenerforsinglevalueevent. Also see https://stackoverflow.com/questions/40190234/firebase-what-is-the-difference-between-setpersistenceenabled-and-keepsynced and https://stackoverflow.com/questions/38664365/firebase-realtime-database-addvalueeventlistener – Frank van Puffelen Jan 01 '18 at 16:28
  • @FrankvanPuffelen thanks it working perfectly now ,one more question, which get fired first : ValueEventListener or ChildEventListener the same location Reference ? – danny Jan 02 '18 at 13:06
  • Child listeners on the same location fire first. You can use that fact to for example add items to a list view from a child listener, and then use a value listener to determine when to hide the progress bar. – Frank van Puffelen Jan 02 '18 at 15:04

0 Answers0