arnesson / cordova-plugin-firebase

Cordova plugin for Google Firebase
http://arnesson.github.io/cordova-plugin-firebase
MIT License
1.01k stars 1.55k forks source link

Getting remote config working on Android + ionic 3 #976

Open nivemaham opened 5 years ago

nivemaham commented 5 years ago

Do not get any response when I try to getValue I am trying to use this plugin to get remote-config working on my ionic 3 app. I have followed documentation, stackoverflow question and other issues, to simply fetch a value. But I do not get any response. Neither success or error message. To Reproduce Steps to reproduce the behavior:

  1. Install and Add plugin as stated here. This is how I am trying to get my remote config value

    this.firebase.fetch(60).then(() => {
        this.firebase.activateFetched().then((value) => {
    
          this.firebase.getValue('projectId').then((proj) => {
            alert('proj' + proj)
          })
          .catch((err) => {
            alert('getvalue err' + err)
          })
        })
        .catch((error2) => {
          alert('activation error' + JSON.stringify(error2));
        });
      }
    );

Expected behavior I expect the value of key or the an error message to catch.

Plugin Version cordova-plugin-firebase 2.0.5

Smartphone (please complete the following information):

What does fetch and activateFetch actually mean? I also noticed if i fetch() without a cacheTimeout specified, then I get false for activateFetch() response. Is this intentional?

Am I doing something wrong? Any help to move this forward is really appreciated. Thanks a lot in advance.

phydiux commented 5 years ago

I'm also using this plugin in an Ionic application and was having issues getting it to work as intended.

The getValue() promise never seems to resolve if you don't pass a string value for the namespace. Unfortunately, passing a namespace to getValue() doesn't actually get any value from Firebase's remote config, it only returns values from setDefaults(), which also requires that you declare a namespace when you set the value. Basically, you can't do either if you want to actually use remote values.

I think this is all further frustrated by the fact that this plugin is listed in the Ionic Framework docs (https://ionicframework.com/docs/native/firebase/) as an Ionic Native plugin, along with some working examples showing that you can access the functions through this.firebase instantiated through the constructor. getValue() isn't listed there but it's not hard to come to the assumption that it would be accessed the same way as any of the other examples, and that it would also work. Unfortunately, it's not working that way.

But after a bit of digging I was able to get the remote configuration for my app by following the advice from Bodeclas in this issue raised: https://github.com/arnesson/cordova-plugin-firebase/issues/339 Specifically, using:

if ((<any>window).FirebasePlugin !== undefined) {
    (<any>window).FirebasePlugin.fetch(60, () => {
      (<any>window).FirebasePlugin.activateFetched(() => {
        console.log('Firebase activateFetched() ran.');
        (<any>window).FirebasePlugin.getValue("SOMEVALUEIWANT", (result) => {
          console.log('Firebase getValue() ran. Result:' + JSON.stringify(result));
        }, (err) => {
          console.log('Firebase getValue() errored. Result:' + JSON.stringify(err));
        });
      }, (err) => {
        console.log("Firebase Error running activateFetched(). Err: " + JSON.stringify(err));
      });
    }, (err) => {
      console.log("Firebase Error running fetch(). Err: " + JSON.stringify(err));
    });
}

In general, accessing this plugin as a Cordova plugin using the (window).FirebasePlugin method worked for me. I hope it works for you.

nivemaham commented 5 years ago

Thanks for sharing @phydiux . I had the same experience. Using native-api resolves the issue. My solution here


declare var window: any;
....
return new Promise((resolve, reject) => {
      window.FirebasePlugin.fetch(600, function () {
        window.FirebasePlugin.activateFetched(function (activated) {
          window.FirebasePlugin.getValue('myTestKey', function (value) {
            resolve(value);
          }, function (error) {
            console.log("FirebasePlugin.getValue error " + error);
            reject(error);
          });
        }, function (error) {
          console.error(error);
          reject(error);
        });
      });
    })
danielsotopino commented 4 years ago

This should totally be reviewed by the ionic team, thanks @phydiux for the info.