2017-05-31 80 views
1

我想在後臺服務(我用來顯示通知)中使用Firebase Remote Config。是否有可能做到這一點 ?Android:是否可以在後臺服務中使用Firebase Remote Config?

爲了更加準確,是否可以在後臺服務中獲取遠程值?

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

    } 
} 

謝謝!

回答

1

回答我自己的問題!是的,這是可能的,只需刪除addOnCompleteListener中的'this'即可。它給出:

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

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

    } 
} 

似乎在後臺服務完美地工作。

+0

就我而言,我試圖在一個不同的進程中通過'IntentService'運行Firebase Remote Config。我不得不從'onStart'函數而不是'onHandleIntent'函數啓動'fetch'命令。 –

+0

'onComplete'在我的情況下永遠不會被調用,你知道爲什麼嗎? – ericn

0

您是否嘗試閱讀手冊? 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(); 
      }