Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use initFirebase() on 1.1.3 [Firebase isn't initialised] [Solutions proposed] #822

Closed
BrainOverflown opened this issue Sep 4, 2018 · 29 comments

Comments

@BrainOverflown
Copy link

BrainOverflown commented Sep 4, 2018

I just updated cordova-plugin-firebase to 1.1.3 and tried to use the initFirebase() method to get rid of the 'Firebase isn't initialised' error (note that the error got a typo in console log).

I'm getting the following error :
Uncaught (in promise): TypeError: _this.firebase.initFirebase is not a function TypeError: _this.firebase.initFirebase is not a function

my code : (note that this code [without initFirebase()] is totally working on another computer.
import {Firebase} from "@ionic-native/firebase"; public start() { return this.platform.ready().then(() => { this.firebase.initFirebase(); this.firebase.getToken().then(token => { console.log(token) this.api.post('tg.php', {token}).subscribe(() => { }); } ).catch(err => console.log(err)); }); }

***** EDIT ***** :

For those encountering the same problem, here is a walkthrough which seems to help most people :

  • downgrading cordova-plugin-firebase to 0.1.18 which can be done like this :
    cordova plugin rm cordova-plugin-firebase
    cordova plugin add cordova-plugin-firebase@0.1.18
    npm install --save @ionic-native/firebase

if it doesn't work

  • uninstalling and reinstalling the plugin with cordova plugin add cordova-plugin-firebase@1.0.5 then npm install @ionic-native/firebase --save

if it doesn't work

declare var FirebasePlugin : any;

and then insert the cordova script include in your index.html file

<script type="text/javascript" src="cordova.js"></script>

and using the plugin like this:

FirebasePlugin.initFirebase();

For those needing a practical example of how to insert it in your own code, here is a snippet courtesy of @tgensol :

observableFromEvent(document, 'deviceready').pipe(first()).subscribe(() => {
FirebasePlugin.initFirebase();
FirebasePlugin.initAnalytics();
})

Depending on how you coded your application, this may have to go in your home.ts /or/ typescript associated with the page calling it /or/ in the provider calling for the getToken() functio.

If the problem isn't solved with this step by step, write down the encountered problems here so we can help troubleshoot for you and others.

[important note : for people having a hard time downgrading, try deleting manually plugins and platforms folders (after backuping them/!), then after plugin installation go for a : cordova platform add android]

Good luck

(follow the thread for credits of contributors)

@BrainOverflown BrainOverflown changed the title Can't use initFirebase() on 1.1.3 Can't use initFirebase() on 1.1.3 [Firebase isn't initialised] Sep 4, 2018
@BrainOverflown
Copy link
Author

Ok when i downgrade to 1.0.5 it seems to work well, but as soon as i get to latest versions it doesn't work. Firebase JS needs to be compatible with plugin version. How can this be properly solved ?

@aetchegoyen
Copy link

Same issue here.

Using:

  • Cordova 8.0.0
  • Cordova Android 7.1.0
  • Firebase Plugin 1.1.3

Thanks

@DomCostantino
Copy link

DomCostantino commented Sep 5, 2018

initFirebase problem should be fixed declaring just below import section:
declare var FirebasePlugin;
and then insert the cordova script include in your index.html file
<script type="text/javascript" src="cordova.js"></script>
and using the plugin like this:
FirebasePlugin.initFirebase();

@BrainOverflown
Copy link
Author

@DomCostantino Doesn't work for me :
"Unexpected token. A constructor, method, accessor, or property was expected."

@tpalts
Copy link

tpalts commented Sep 5, 2018

I had this same issue. Got rid of the 'Firebase isn't initialised' error by downgrading to 1.0.5:

cordova plugin remove cordova-plugin-firebase
cordova plugin add cordova-plugin-firebase@1.0.5

@SimonKomlos
Copy link

Same problem. I created a stackoverflow question with a 50 reputation bounty

https://stackoverflow.com/questions/52140850/firebase-isnt-initialised-on-ios-ionic/52186436

@soumak77
Copy link
Contributor

soumak77 commented Sep 5, 2018

@SimonKomlos this might be because iOS is still on firebase SDK 4.13.0 (see #796). I believe android is on the latest firebase version.

@BrainOverflown
Copy link
Author

@soumak77 i don't think so, i have the same problem on Android with latest updates. as i told Firebase JS needs to be compatible with plugin version.

@soumak77
Copy link
Contributor

soumak77 commented Sep 5, 2018

@BrainOverflown I was specifically talking about the stackoverflow question @SimonKomlos posted. Though if v1.0.5 works, then I'm wrong since it's also on v4.13.0 for iOS.

@BrainOverflown
Copy link
Author

@soumak77 got it ! Solution to this problem will make a lot of happy people.

People should try downgrading to 0.1.18 as advised on the Stackoverflow thread :

cordova plugin remove cordova-plugin-firebase cordova plugin add cordova-plugin-firebase@0.1.18 --save

@SimonKomlos
Copy link

@BrainOverflown did downgrading fix the problem for you?

@BrainOverflown
Copy link
Author

@SimonKomlos Yes, but people should be very careful because when you install by editing your config.xml, it will automatically be renammed from "1.0.5" to "^1.0.5" (this caracter is making cordova build the platform with latest version and not 1.0.5).

@SimonKomlos
Copy link

@BrainOverflown Okay, thanks. Did you do 1.0.5 or 0.1.18?

@toyotathon
Copy link

Downgrading to 1.0.5 worked for me!

@BrainOverflown
Copy link
Author

@SimonKomlos I tried 1.0.5 and it does work. But for some configurations going down to 0.1.18 may be useful so people reading this better try both if 1.0.5 doesn't work i guess..

@gooru-ajay
Copy link

while downgrading to 1.0.5, firebase getToken function throws an error like class not found

please suggest me in which down version getToken will work?

@BrainOverflown
Copy link
Author

BrainOverflown commented Sep 5, 2018

@ajaykumar2897 Test this different steps :

  • downgrading to 0.1.18
    if it doesn't work
  • uninstalling and reinstalling the plugin with cordova plugin add cordova-plugin-firebase@1.0.5 then npm install @ionic-native/firebase --save
  • if it doen't work try to upgrade to 1.1.3 and try @DomCostantino trick :

declare var FirebasePlugin : any;

and then insert the cordova script include in your index.html file

<script type="text/javascript" src="cordova.js"></script>

and using the plugin like this:

FirebasePlugin.initFirebase();

If it's not ok give encountered problems here so we can help troubleshoot.

Good luck

@willmero
Copy link

willmero commented Sep 5, 2018

Was able to use the FirebasePlugin.getToken() function after I called FirebasePlugin.initFirebase() successfully, though if I try to push out a notification through the FCM console, I get an error stating Unregistered registration token. Hmm.

@tgensol
Copy link

tgensol commented Sep 6, 2018

Based on @BrainOverflown, I managed to make it working (the 1.1.3 version) by calling these methods in my app.components.ts, just after the device is ready

observableFromEvent(document, 'deviceready').pipe(first()).subscribe(() => {
FirebasePlugin.initFirebase();
FirebasePlugin.initAnalytics();
})

You also need to declare Firebase Plugin to use it

declare var FirebasePlugin: any;

Best

@BrainOverflown
Copy link
Author

@tgensol Thanks, i updated my previous post with the same variable declaration as yours for consistency.

@BrainOverflown BrainOverflown changed the title Can't use initFirebase() on 1.1.3 [Firebase isn't initialised] Can't use initFirebase() on 1.1.3 [Firebase isn't initialised] [Solutions proposed] Sep 6, 2018
@soumak77
Copy link
Contributor

soumak77 commented Sep 6, 2018

It appears the root cause of this issue as determined by @briantq was that the Crashlytics PR (#784) removed the auto-init functionality. Would any be able to help investigate and figure out what code needs to be added back to fix this? I would love to accept a PR and push out a new patch release so that those adopting v1.1.x don't have to change their code to get the plugin to work as it used to work (i.e. no breaking changes).

@briantq
Copy link
Contributor

briantq commented Sep 6, 2018

For anyone having this issue, could you please try testing with a branch for a proposed Pull Request PR#830. Run cordova plugin add https://github.com/briantq/cordova-plugin-firebase.git#init-rollback to install the proposed branch. Assuming this resolves the issue, we will merge this branch into master and release an updated version of the plugin.

If you could confirm whether or not this resolves the issue, that would be helpful. I want to make sure people don't work around this issue instead of just solving it. We are going to be removing the initFirebase() method ASAP so if you do modify your code to work around it, you will have to undo the modification in the coming days.

@bpdinteractive
Copy link

@briantq, working here.

$ ng info

Angular CLI: 1.7.4
Node: 7.6.0
OS: darwin x64
Angular: 5.0.0
... animations, common, compiler, compiler-cli, core, forms
... http, platform-browser, platform-browser-dynamic

@angular/cdk: 5.2.4
@angular/material: 5.2.4
@angular-devkit/build-optimizer: 0.0.35
typescript: 2.4.2
webpack: 3.8.1

$ ionic info


cli packages: (/usr/local/lib/node_modules)

    @ionic/cli-utils  : 1.9.2
    ionic (Ionic CLI) : 3.9.2

global packages:

    Cordova CLI : 8.0.0 

local packages:

    @ionic/app-scripts : 3.2.0
    Cordova Platforms  : android 7.0.0
    Ionic Framework    : ionic-angular 3.9.2

System:

    Android SDK Tools : 26.0.1
    ios-deploy        : 1.9.2 
    Node              : v7.6.0
    npm               : 4.2.0 
    OS                : macOS High Sierra
    Xcode             : Xcode 9.4.1 Build version 9F2000 

package.json

{
  "name": "ionic-v2",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "http://ionicframework.com/",
  "private": true,
  "scripts": {
    "clean": "ionic-app-scripts clean",
    "build": "ionic-app-scripts build",
    "lint": "ionic-app-scripts lint",
    "ionic:build": "ionic-app-scripts build",
    "ionic:serve": "ionic-app-scripts serve"
  },
  "dependencies": {
    "@angular/animations": "5.0.0",
    "@angular/cdk": "5.2.4",
    "@angular/common": "5.0.0",
    "@angular/compiler": "5.0.0",
    "@angular/compiler-cli": "5.0.0",
    "@angular/core": "5.0.0",
    "@angular/forms": "5.0.0",
    "@angular/http": "5.0.0",
    "@angular/material": "5.2.4",
    "@angular/platform-browser": "5.0.0",
    "@angular/platform-browser-dynamic": "5.0.0",
    "@ionic-native/call-number": "^4.12.2",
    "@ionic-native/core": "4.3.2",
    "@ionic-native/file": "^4.12.2",
    "@ionic-native/file-opener": "^4.12.2",
    "@ionic-native/file-transfer": "^4.12.2",
    "@ionic-native/fingerprint-aio": "^4.12.2",
    "@ionic-native/firebase": "^4.12.2",
    "@ionic-native/splash-screen": "4.3.2",
    "@ionic-native/status-bar": "4.3.2",
    "@ionic/storage": "2.1.3",
    "angularfire2": "5.0.0-rc.3",
    "call-number": "^1.0.1",
    "cordova-android-support-gradle-release": "^1.4.4",
    "cordova-plugin-device": "^1.1.7",
    "cordova-plugin-file": "^6.0.1",
    "cordova-plugin-file-opener2": "^2.0.19",
    "cordova-plugin-file-transfer": "^1.7.1",
    "cordova-plugin-fingerprint-aio": "^1.3.8",
    "cordova-plugin-firebase": "git+https://github.com/briantq/cordova-plugin-firebase.git#init-rollback",
    "cordova-plugin-ionic-webview": "^1.2.1",
    "cordova-plugin-splashscreen": "^4.1.0",
    "cordova-plugin-whitelist": "^1.3.3",
    "cordova-sqlite-storage": "^2.4.0",
    "firebase": "4.6.0",
    "ionic-angular": "3.9.2",
    "ionic-plugin-keyboard": "^2.2.1",
    "ionicons": "3.0.0",
    "libphonenumber-js": "^1.4.4",
    "mx.ferreyra.callnumber": "0.0.2",
    "rxjs": "5.5.2",
    "sw-toolbox": "3.6.0",
    "zone.js": "0.8.18",
    "cordova-android": "~7.0.0"
  },
  "devDependencies": {
    "@ionic/app-scripts": "3.2.0",
    "typescript": "2.4.2"
  },
  "config": {
    "ionic_copy": "./config/copy.config.js"
  },
  "description": "An Ionic project",
  "cordova": {
    "plugins": {
      "ionic-plugin-keyboard": {},
      "cordova-plugin-whitelist": {},
      "cordova-plugin-device": {},
      "cordova-plugin-splashscreen": {},
      "cordova-plugin-ionic-webview": {},
      "cordova-sqlite-storage": {},
      "cordova-plugin-file-opener2": {},
      "cordova-plugin-file-transfer": {},
      "cordova-plugin-file": {},
      "cordova-android-support-gradle-release": {
        "ANDROID_SUPPORT_VERSION": "27.+"
      },
      "mx.ferreyra.callnumber": {},
      "cordova-plugin-fingerprint-aio": {},
      "call-number": {},
      "cordova-plugin-firebase": {}
    },
    "platforms": [
      "android"
    ]
  }
}

@aetchegoyen
Copy link

aetchegoyen commented Sep 7, 2018

Guys, confirmed.
Using Cordova 8, Cordova Android 7.1.1 and Cordova Plugin Firebase 1.1.3:

You just need to add the FirebasePlugin.initFirebase method before using Firebase. And then in it's success callback get your token:

IE:

//Inits Firebase
FirebasePlugin.initFirebase(
    function(){
        //Subscribes for token refresh event
        window.FirebasePlugin.onTokenRefresh(
            function(token) {
                console.log("Device token refresh: " + token);
                //Your token handling functions
            }, 
            function(error) {
                console.error("Firebase error: " + error);
            }
        );
    }, 
    function(error) {
        console.error("Firebase error: " + error);
    }
);

@soumak77
Copy link
Contributor

soumak77 commented Sep 7, 2018

For all those running into this issue, it was caused by a PR which removed auto-init functionality. This is being put back in place via #830, which will remove the unnecessary initFirebase method. This means your code will need to revert back to what was working with v1.0.5 and earlier. I apologize for this inconvenience, but we need to get the plugin working as it had been prior to the v1.1.0 release since removable of the auto-init functionality broke a ton of apps.

@qswinson
Copy link

qswinson commented Sep 7, 2018

I have verified the pull request removes the need to call initFirebase for both iOS and Android however it does still require initAnalytics to be added if you want that feature enabled. I'm not sure if this is a breaking change from the previously released version but it was for the very old version I updated from (0.1.25).

@briantq
Copy link
Contributor

briantq commented Sep 7, 2018

@qswinson I was just thinking about the same thing, especially since there are two bugs that were opened in the past couple days about analytics not working. I feel like maybe we should enable those by default but allow some parameter that you can use to turn them off. I would expect many people would expect the APIs to just work which could cause confusion, even if we document that the feature is disabled by default. @soumak77 do you have an opinion on on/off be default?

@soumak77
Copy link
Contributor

soumak77 commented Sep 7, 2018

It seems like the plugin would be easier to adopt if everything just worked right away. For advanced users, they can choose which features should be turned off based on their app's requirements. With that said, we should add some cordova configuration variables which allows disabling certain features.

@soumak77
Copy link
Contributor

soumak77 commented Sep 8, 2018

This should be fix via v1.1.4. Please be sure to read my comment above needing to revert some changes to make it work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests