2

I would like to use Firebase Remote Config in a background service (that I used to display notifications). Is it possible to do this ?

To be more accurate, is it possible to fetch the remote values in a background service ?

// Code below in a background service ?
mFirebaseRemoteConfig.fetch(cacheExpiration).addOnCompleteListener(this, new OnCompleteListener<Void>() {

    @Override
    public void onComplete(@NonNull Task<Void> task) {
             // get remote values here            

    }
 }

Thanks !!!

AL.
  • 36,815
  • 10
  • 142
  • 281
toto_tata
  • 14,526
  • 27
  • 108
  • 198

2 Answers2

5

Answer to my own question ! YES, it is possible, simply remove the 'this' in the addOnCompleteListener. It gives :

mFirebaseRemoteConfig.fetch(cacheExpiration).addOnCompleteListener(new OnCompleteListener<Void>() {

    @Override
    public void onComplete(@NonNull Task<Void> task) {
             // get remote values here            

    }
 }

Seems to work perfectly in a background service.

toto_tata
  • 14,526
  • 27
  • 108
  • 198
  • In my case, I was trying to run Firebase Remote Config from an `IntentService` in a different process. I had to fire the `fetch` command from the `onStart` function rather than from the `onHandleIntent` one. – Cigogne Eveillée Jul 14 '17 at 18:37
  • `onComplete` is never called in my case, do you know why? – ericn Jul 31 '17 at 07:55
-1

Have you tried to read manual? https://firebase.google.com/docs/remote-config/android

 @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Toast.makeText(MainActivity.this, "Fetch Succeeded",
                            Toast.LENGTH_SHORT).show();

                    // After config data is successfully fetched, it must be activated before newly fetched
                    // values are returned.
                    mFirebaseRemoteConfig.activateFetched();
                } else {
                    Toast.makeText(MainActivity.this, "Fetch Failed",
                            Toast.LENGTH_SHORT).show();
                }
                displayWelcomeMessage();
            }
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • 1
    You conveniently ignored the line directly before what you copied: `.addOnCompleteListener(this, new OnCompleteListener() {`. `this` in that context is an activity. – Niall Aug 08 '18 at 10:28