How to use cordova-plugin-firebase? (Ionic 2)

Hello people,

I am required to integrate Firebase Analytics to the application I am working on.
I found this plugin: https://github.com/arnesson/cordova-plugin-firebase
Which I downloaded. I also downloaded the configuration file from the Firebase project panel and saved it in the root directory as it says in the plugin repository.

But now I do not know how to continue, did someone use it? How do I use it inside the app?

Thanks in advance!

Did you look at the docs of the plugin?

Here are the analytics functions you can use:

Well, it result that this plugin now is official: http://ionicframework.com/docs/v2/native/firebase/

But i still having problems. If I do what the example says:

import { Firebase } from 'ionic-native';

Firebase.getToken()
  .then(token => console.log(`The token is ${token}`)) // save the token server-side and use it to push notifications to this device
  .catch(error => console.error('Error getting token', error));

Firebase.onTokenRefresh()
  .subscribe((token: string) => console.log(`Got a new token ${token}`));

I get this errors:

"blablabla/ionic-native/dist/es5/index" has no exported member 'Firebase'.

And therefore:

Error: Uncaught (in promise): TypeError: Cannot read property 'getToken' of undefined

What is the benefit of running the native plugin? It isn’t required. I looked at it and couldn’'t understand why I needed it, so I didn’t install it. Not a snarky question – I genuinely don’t understand why it’s better than just following the instructions on the Firebase home page.

Ok, with this specific error message I think someone that knows a bit about Ionic2 will be able to help you. (I don’t, so good luck.)

How did you include analytics from the firebase instructions? Because, as I see it, analytics functions are only available for Android and IOS native, not for the web. And in ionic 2 we work everything as if it were web.
That’s why I tried to use the plugin, because otherwise I can not find a way to do it.

If you did it by following the instructions from firebase, could you tell me how you did it?
Thanks for answering!

Hi Aaron,

I’m trying to get Firebase Analytics running on mi Ionic 2 app and am running into the same issue that Colo9311 mentions. Can you explain which plugin did you use and how you got it working?

Thanks in advance,

Leo

I got the same error with “blablabla/ionic-native/dist/es5/index” has no exported member ‘Firebase’.

I try to rebuild ionic project, then find the plugin work!!

Hi @kiko555, can you explain me how to rebuild ionic project? So I can also use the plugin :slight_smile:

after execute “ionic state reset”,
I saw the Firebase in “blablabla/ionic-native/dist/es5/index”.

If you don’t want change the project right now.
I suggest you try to create a new ionic project, then add the plugin and check import.

I suppose the “blablabla/ionic-native/dist/es5/index” not change with the “ionic plugin add cordova-plugin-firebase”

I am facing the same problem.

To make this plugin work with ionic 2, my workaround is the following :

  • Install the plugin :
    ionic plugin add cordova-plugin-badge

  • Add this line in /src/declaration.d.ts
    interface Window { FirebasePlugin: any; }

  • And follow the instructions on the github page of the plugin. For instance :
    window.FirebasePlugin.getToken(function(token) { console.log(token); }, function(error) { console.error(error); });

The ionic-native doc is not compliant with this way of importing the plugin.

Are all of you certain that you have >=2.3.0 of ionic-native when the Firebase plugin was added? There should be no need for Window-based kludgery.

Hi Colo9311,

Have you done the analytics task? Can you share how you have done it? Thanks.

Nope, I finally used Facebook analytics wich worked for me.

I have done that.Please see this Gist.

Thank you!! :smiley:

You’re warmly welcome :slight_smile: