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

Default FirebaseApp is not initialized #816

Closed
bnfrnz opened this issue Sep 1, 2018 · 37 comments
Closed

Default FirebaseApp is not initialized #816

bnfrnz opened this issue Sep 1, 2018 · 37 comments

Comments

@bnfrnz
Copy link

bnfrnz commented Sep 1, 2018

After updating to plugin version 1.1.0, app crashes immediately at launch on Android devices.

Log excerpt:

09-01 13:01:12.929 25125-25125/com.xxx.xxx E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.xxx.xxx, PID: 25125
    java.lang.ExceptionInInitializerError
        at com.google.android.gms.internal.firebase-perf.zzw.zzae(Unknown Source)
        at com.google.firebase.perf.metrics.Trace.start(Unknown Source)
        at com.google.android.gms.internal.firebase-perf.zze.onActivityStarted(Unknown Source)
        at android.app.Application.dispatchActivityStarted(Application.java:207)
        at android.app.Activity.onStart(Activity.java:1191)
        at org.apache.cordova.CordovaActivity.onStart(CordovaActivity.java:297)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1249)
        at android.app.Activity.performStart(Activity.java:6701)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2647)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6186)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
     Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.xxx.xxx. Make sure to call FirebaseApp.initializeApp(Context) first.
        at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.1:219)
        at com.google.android.gms.internal.firebase-perf.zzw.<init>(Unknown Source)
        at com.google.android.gms.internal.firebase-perf.zzw.<clinit>(Unknown Source)

I realize this may be a duplicate of #142, #715 or #206 but those are closed and meant to be fixed.

We have been using the plugin for quite a while and never actually encountered this before. It's only occurring in plugin version 1.1.0 for us. We have downgraded to version 1.0.5 again and now everything works again. So, I thought I'd open a new issue for this as there may be some regression error. Happy to provide more info if there are questions...

@remisture
Copy link

+1

@strttn
Copy link

strttn commented Sep 2, 2018

On iOS I was getting the error "Firebase isn't initialised" when calling the getToken method.
I also managed to get things working by downgrading to v1.0.5 (which my Android version was already running and was working fine with, as far as I could tell).

@vuneotwist
Copy link

+1 I have the same problem

@liveocr
Copy link

liveocr commented Sep 2, 2018

  1. The same issue with v1.1.2
  2. On iOS 11.4 downgrading to v1.0.5 gives no notifications received.
  3. getToken() gives non empty token.

@CWollinger
Copy link

Get "Firebase isn't initialised" on Android after the DeviceReady,too

@Hyperblaster
Copy link

+1 Same issue, Was working 2 weeks ago, Just installed it and now android won't compile. iOS is fine though

@gvieduc
Copy link
Contributor

gvieduc commented Sep 3, 2018

In an GDPR approch you have to let users choose if they want or not to upload data to Firebase services. So Firebase haven't to be initialized by default.

Look at the code and use following methods to initialize services before using them :
exports.initFirebase
exports.initCrashlytics
exports.initAnalytics
exports.initPerformance
exports.initRemoteConfig

For my own I still using my fork which is working fine. I didn't have time to check if all merged PR are working.

@CWollinger
Copy link

Adding window.FirebasePlugin.initFirebase(); before getToken worked for me.

@rob1nxu
Copy link

rob1nxu commented Sep 3, 2018

@CWollinger thanks! adding window.FirebasePlugin.initFirebase() worked for me too. i am using version 1.1.2

@Medtrack
Copy link

Medtrack commented Sep 3, 2018

Calling window.FirebasePlugin.initFirebase() didn't work for me. App crashes just at startup.

Reviewing the plugin, version 1.0.5 I found this, :
Context context is defined and FirebaseApp.initializaApp(context) called on pluginitialize thread.

Edit,
I just read comment @gvieduc comment I I see that Firebase is not initialized by default.
I will recheck my code in case I made a mistake and, in case I can't fix it, I will downgrade.

@8tomo8
Copy link

8tomo8 commented Sep 3, 2018

Thanks for the fix @CWollinger
For Ionic 3 users add (<any>window).FirebasePlugin.initFirebase() before getToken

@Drintios
Copy link
Contributor

Drintios commented Sep 3, 2018

Looks like issue is related to this https://stackoverflow.com/questions/37321728/firebaseinitprovider-firebaseapp-initialization-unsuccessful we need google services on gradle.build file

@BrainOverflown
Copy link

BrainOverflown commented Sep 3, 2018

Got the "Firebase isn't initialised" on Android. I have two computers, and one Ionic project : When i npm install > add android platform on the first one, then build : no problem, getToken() gives me the token. But on the second one i keep having the error even after 1/ doing the same exact steps, 2/ messing with MainActivity.java / push in build.gradle or manually adding dependencies.
The only differences i have on the 2 computers is Android Studio Versions & gradle.

@8tomo8 when you say : (<any>window).FirebasePlugin.initFirebase() where do i have to put this code exactly ? in which file ? Because my getToken() function is called in .ts file.

@rubytann
Copy link

rubytann commented Sep 4, 2018

@8tomo8 I have the same question. I added (<any>window).FirebasePlugin.initFirebase() in app.components.ts, my app just froze. I'm using ionic 3 too. Could you please explain where I should use it?

@8tomo8
Copy link

8tomo8 commented Sep 4, 2018

@BrainOverflown @rubytann I am using iOS so Android would look slightly different, but here is my getToken() function that is working 100% on emulator and device. The code is in a provider that is called on the ionViewDidEnter() lifestyle hook on the root page.

async getToken() { (<any>window).FirebasePlugin.initFirebase() let token; if (this.platform.is('ios')) { token = await this.fireNative.getToken(); const perm = await this.fireNative.grantPermission() } return this.saveToken(token) }

@BrainOverflown
Copy link

BrainOverflown commented Sep 4, 2018

@8tomo8 Doesn't work for me, i got this error :

Uncaught (in promise): TypeError: (<any>window).FirebasePlugin.initFirebase(); is not a function
TypeError: (<any>window).FirebasePlugin.initFirebase(); is not a function

@8tomo8
Copy link

8tomo8 commented Sep 4, 2018

@BrainOverflown this isn't a suggested solution, but rather just isolating the problem. Can you put the getToken inside a timeout of 3 seconds from the ionViewDidEnter() and see if it makes a difference? I have had this before.

ionViewDidEnter() { setTimeout(() => { //call your get token from your provider here },3000) }

@BrainOverflown
Copy link

Did not change anything. "Firebase isn't initialised" still.

The problem is that i can build with "Firebase working well" on one computer, but not the second one.

Here is my code :

home.ts

  ionViewWillEnter() {
    this.platform.ready().then(() => {
      this.statusBar.show();
    });
	this.firebaseNotifications.start().then(() => {
      this.firebaseNotifications.onNotifications((data) => {
        // data received
      })
    });
  }

Which call on this provider : firebase-poc.ts

   platform.ready().then(() => {
	  
      firebase.getToken().then(token => console.log(token)).catch(err => console.log(err));
      firebase.onNotificationOpen().subscribe(data => {
        console.log(data);
        console.log(data.name)
        const toast = this.toastCtrl.create({
          message: data.body,
          duration: 3000
        });
        toast.present();
      }, err => console.log(err));
    });
  }

@Drintios
Copy link
Contributor

Drintios commented Sep 4, 2018

Created pull request to fix google services issue, which causes app crash, after this with latest changes we need to do window.FirebasePlugin.initFirebase()

@BrainOverflown
Copy link

@Drintios So basically, what you're advising is to update cordova-plugin-firebase to it's latest version which support the initFirebase method ?

@Drintios
Copy link
Contributor

Drintios commented Sep 4, 2018

yep, with my PR it shouldn't crash anymore

soumak77 added a commit that referenced this issue Sep 4, 2018
#816 Fix for google services issue (Firebase not initialized)
@soumak77
Copy link
Contributor

soumak77 commented Sep 4, 2018

fix has been published via v1.1.3

@BrainOverflown
Copy link

You should reopen it, i still have the problem

@soumak77
Copy link
Contributor

soumak77 commented Sep 4, 2018

@BrainOverflown would you suggest also reverting the PR?

@soumak77 soumak77 reopened this Sep 4, 2018
@BrainOverflown
Copy link

No, since my problem is not related to the fix made by @Drintios .

Just let more people to test the commit and we'll see. Thank you

@toyotathon
Copy link

I am currently running into this issue now, this is happening on an iOS device. Have not tested this on an Android device.

@Drintios
Copy link
Contributor

Drintios commented Sep 4, 2018

App crashing/not starting ? or issue with init firebase in js console ?

@toyotathon
Copy link

Just with the init firebase part. I tried using the (<any>window).FirebasePlugin.initFirebase() solution, but that has been giving me a type error. I am building with Ionic 3.

@Drintios
Copy link
Contributor

Drintios commented Sep 4, 2018

i see, i think we need to open need thread for this issue as it not related to app crash.

@BrainOverflown
Copy link

I already did @Drintios

@Drintios
Copy link
Contributor

Drintios commented Sep 4, 2018

great :)

@toyotathon
Copy link

Thanks @BrainOverflown , I'll follow your thread!

@soumak77
Copy link
Contributor

soumak77 commented Sep 4, 2018

Closing per the community discussion

@soumak77 soumak77 closed this as completed Sep 4, 2018
@eduardo-k-araujo
Copy link

On ionic v3 i created a provider for Analytics.. Works for me!

`import { Injectable } from '@angular/core';
import { Firebase } from '@ionic-native/firebase';

@Injectable()
export class AnalyticsProvider {

constructor(private firebaseCtrl: Firebase) { }

private initAnalytics(callback) {
    (<any>window).FirebasePlugin.initAnalytics();
    setTimeout(() => {
        if (callback) callback();
    }, 100);
}

setScreenName(name: string): void {
    this.firebaseCtrl.setScreenName(name).then(
        data => console.log('firebaseCtrl > setCurrentScreen > success', data),
        error => {
            console.log('firebaseCtrl > setCurrentScreen > error', error);
            if (error == "Analytics isn't initialised") {
                this.initAnalytics(() => this.setScreenName(name));
            }
        }
    ).catch(exception => console.error('firebaseCtrl > setCurrentScreen > exception', exception));
}

logEvent(event: string, data: any) {
    this.firebaseCtrl.logEvent(event, data).then(
        data => console.log('firebaseCtrl > logEvent > success', data),
        error => {
            console.log('firebaseCtrl > logEvent > error', error);

            if (error == "Analytics isn't initialised") {
                this.initAnalytics(() => this.logEvent(event, data));
            }
        }
    ).catch(exception => console.error('firebaseCtrl > logEvent > exception', exception));
}

}`

@tgensol
Copy link

tgensol commented Sep 6, 2018

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;

@soumak77
Copy link
Contributor

soumak77 commented Sep 7, 2018

This is being fixed via #830. Hopefully the community can help get the PR tested soon so I can release the fix.

@briantq briantq mentioned this issue Sep 15, 2018
@sergioutama
Copy link

Unfortunately this is still exist on android

I need to support this legacy code and include firebase, all good on iOS just on Android it's always the same error. I checked the source code and indeed the firebase init code was there.

Any lead is highly appreciated.


Ionic:

   Ionic CLI          : 5.2.3
   Ionic Framework    : ionic-angular 3.9.5-201905161949
   @ionic/app-scripts : 3.2.3

Cordova:

   Cordova CLI       : 8.1.2 ([email protected])
   Cordova Platforms : android, ios 4.5.5
   Cordova Plugins   : cordova-plugin-ionic-webview 1.2.1, (and 34 other plugins)

Utility:

   cordova-res : 0.6.0 
   native-run  : 0.2.8 

System:

   Android SDK Tools : 26.1.1 
   ios-deploy        : 1.9.2
   NodeJS            : v11.10.1 
   npm               : 6.7.0
   OS                : macOS Mojave
   Xcode             : Xcode 10.1 Build version 10B61

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