远程配置不会在onStart()上重新获取数据

时间:2019-09-28 11:10:42

标签: android firebase-remote-config

我有一个名为“ min_version” 远程配置变量,该变量指示我不是用户拥有的最低版本(如果他们的版本低于该版本)-更新警报将弹出。问题在于,当更新Firebase上的远程配置时,该信息不适用于onStart()上的应用程序,并且不会提示应用程序内更新功能。仅当进入应用程序设置并按“清除数据”时,它才能使获取信息的信息正确刷新,但是没有用户会这样做...

这是我的代码-


@Override
  protected void onStart() {
    super.onStart();
    // navigation drawer
    checkValidFacebookSession();
    initDrawerMenu();

    // network monitoring
    registerNetworkReceiver();

    // monitoring upload
    LocalBroadcastManager.getInstance(this).registerReceiver(mUploadReceiver, new IntentFilter(ULBroadcastConstants.UPLOAD_STATUS_ACTION));
    LocalBroadcastManager.getInstance(this).registerReceiver(mFCMReceiver, new IntentFilter(MyFirebaseMessagingService.RECEIVED_FCM_ACTION));

    checkInAppUpdate();

  }

private void checkInAppUpdate() {

    AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(App.getAppContext());
    Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
    appUpdateInfoTask.addOnSuccessListener(appUpdateInfo ->  {
      checkUpdateInProgress(appUpdateManager, appUpdateInfo);
      checkUpdateAvailable(appUpdateManager, appUpdateInfo);
    });


  }

  private void checkUpdateInProgress(AppUpdateManager appUpdateManager, AppUpdateInfo appUpdateInfo) {
    if (appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
      //if an  in app update is already running - resume the update
      try {
        appUpdateManager.startUpdateFlowForResult(appUpdateInfo, AppUpdateType.IMMEDIATE, this, VERSION_UPDATE_REQUEST_CODE);
      } catch (IntentSender.SendIntentException e) {
        e.printStackTrace();
      }
    }
  }


  private void checkUpdateAvailable(AppUpdateManager appUpdateManager, AppUpdateInfo appUpdateInfo) {
    getMinAppVersion(() -> {
      int currentVersionCode = BuildConfig.VERSION_CODE;
      if (currentVersionCode < min_version && appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
        try {
          appUpdateManager.startUpdateFlowForResult(appUpdateInfo, AppUpdateType.IMMEDIATE, this, VERSION_UPDATE_REQUEST_CODE);
        } catch (IntentSender.SendIntentException e) {
          e.printStackTrace();
        }
      }
    });
  }

private void getMinAppVersion(OnRemoteConfigFetchComplete listener){ // <- this is the function that gets the remote config information and fails (returns the last saved variable) without cleaning application data. 
    //fetching the min_version parameter from 'remote config' of Firebase and saves it to our local variable.
    FirebaseRemoteConfig mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
    FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder().setMinimumFetchIntervalInSeconds(200).build();
    mFirebaseRemoteConfig.setConfigSettingsAsync(configSettings);
    mFirebaseRemoteConfig.fetchAndActivate().addOnCompleteListener(task -> {
      if (task.isSuccessful()) {
        min_version = mFirebaseRemoteConfig.getLong(RemoteConfigUtil.MIN_VERSION);
        listener.onFetchComplete();
      } else {
        Timber.tag("min version").d("error while fetching and activating remove config");
      }
    });
  }


0 个答案:

没有答案