cordova-plugin-firebasex
cordova-plugin-firebasex copied to clipboard
FirebasePlugin._onAuthIdTokenChange() fires before device ready, FirebasePlugin undefined
Bug report
FirebasePlugin._onAuthIdTokenChange() is called before device ready, FirebasePlugin undefined at that time. After device ready, FirebasePlugin is defined.
Expected behavior: FirebasePlugin._onAuthIdTokenChange() to start after device is ready
Screenshots
Environment information Cordova 12.0.0 ([email protected]) [email protected] Windows 10 x64 Pixel 4a with Android 13 Node v20.9.0
Code
<plugin name="cordova-plugin-device" spec="^2.1.0" />
<plugin name="cordova-plugin-statusbar" spec="^4.0.0" />
<plugin name="cordova-plugin-network-information" spec="^3.0.0" />
<plugin name="cordova-plugin-camera" spec="^7.0.0" />
<plugin name="cordova-plugin-buildinfo" spec="^4.0.0" />
<plugin name="cordova-custom-config" spec="^5.1.1" />
<plugin name="cordova-plugin-androidx-adapter" spec="^1.1.3" />
<plugin name="cordova-plugin-inappbrowser" spec="^6.0.0" />
<plugin name="cordova-plugin-geolocation" spec="^5.0.0" />
<plugin name="cordova-plugin-file" spec="^8.0.1" />
<plugin name="cordova-plugin-firebasex" spec="^16.5.0">
<variable name="ANDROID_ICON_ACCENT" value="#742A84" />
<variable name="IOS_STRIP_DEBUG" value="true" />
<variable name="FIREBASE_ANALYTICS_COLLECTION_ENABLED" value="false" />
<variable name="FIREBASE_PERFORMANCE_COLLECTION_ENABLED" value="false" />
<variable name="FIREBASE_CRASHLYTICS_COLLECTION_ENABLED" value="false" />
</plugin>
I removed all plugins, restarted on a clean project and it works, so it's due to another plugin.
Leaving this as information for the next guy, this breaks everything
<plugin name="cordova-plugin-network-information" spec="^3.0.0" />
<plugin name="cordova-plugin-inappbrowser" spec="^6.0.0" />
<plugin name="cordova-plugin-geolocation" spec="^5.0.0" />
Reopening hoping someone is facing the same problem. Most of the official plugins break this plugin and this sounds pretty odd to me.
Closing again cause it randomly breaks or works. Looks like cordova-android or cordova at large is not stable anymore. I have no idea what's going on.
Aaaand reopening again. Firebasex alone with no other plugin works. But then adding other standard plugins break FirebasePlugin. I even uninstalled and reinstalled cordova, restarted the project from scratch and I always end up at the same place.
Would anyone be kind enough to try to build with the following plugins
<plugin name="cordova-plugin-device" spec="^2.1.0" />
<plugin name="cordova-plugin-statusbar" spec="^4.0.0" />
<plugin name="cordova-plugin-camera" spec="^7.0.0" />
<plugin name="cordova-plugin-inappbrowser" spec="^6.0.0" />
<plugin name="cordova-plugin-geolocation" spec="^5.0.0" />
<plugin name="cordova-plugin-firebasex" spec="^16.5.0">
<variable name="ANDROID_ICON_ACCENT" value="#742A84" />
<variable name="IOS_STRIP_DEBUG" value="true" />
<variable name="FIREBASE_ANALYTICS_COLLECTION_ENABLED" value="false" />
<variable name="FIREBASE_PERFORMANCE_COLLECTION_ENABLED" value="false" />
<variable name="FIREBASE_CRASHLYTICS_COLLECTION_ENABLED" value="false" />
</plugin>
This breaks here 100% of the time and give the issue described in the OP.
PS : same thing happens when I add plugins to your test app
Have the same error after update plugin to version 16.5.0
Could be related to https://github.com/apache/cordova-android/pull/1605
Opened an issue here : https://github.com/apache/cordova-android/issues/1715
I could fix this issue by adding a timeout of 2000ms the first time it runs, then 0ms once it has run. This solves the racing issue when having other plugins. Not creating a pull request because this is more of a POC than anything else and not the best in Java, and I don't know the code enough....
```
private static int initTimeout = 2000;
public static void setTimeout(Runnable runnable, int delay){
new Thread(() -> {
try {
Thread.sleep(delay);
runnable.run();
}
catch (Exception e){
System.err.println(e);
}
}).start();
}
public void onIdTokenChanged(@NonNull FirebaseAuth firebaseAuth) {
try {
FirebaseUser user = firebaseAuth.getCurrentUser();
user.getIdToken(true).addOnSuccessListener(new OnSuccessListener<GetTokenResult>() {
@Override
public void onSuccess(GetTokenResult result) {
try {
String idToken = result.getToken();
if (idToken != null && idToken.equals(instance.currentIdToken)) {
return;
}
instance.currentIdToken = idToken;
String providerId = result.getSignInProvider();
setTimeout(() -> FirebasePlugin.instance.executeGlobalJavascript(JS_GLOBAL_NAMESPACE + "_onAuthIdTokenChange({\"idToken\":\"" + idToken + "\",\"providerId\":\"" + providerId + "\"})"), FirebasePlugin.initTimeout);
} catch (Exception e) {
setTimeout(() -> FirebasePlugin.instance.executeGlobalJavascript(JS_GLOBAL_NAMESPACE + "_onAuthIdTokenChange()"), FirebasePlugin.initTimeout);
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
setTimeout(() -> FirebasePlugin.instance.executeGlobalJavascript(JS_GLOBAL_NAMESPACE + "_onAuthIdTokenChange()"), FirebasePlugin.initTimeout);
}
});
} catch (Exception e) {
setTimeout(() -> FirebasePlugin.instance.executeGlobalJavascript(JS_GLOBAL_NAMESPACE + "_onAuthIdTokenChange()"), FirebasePlugin.initTimeout);
}
FirebasePlugin.initTimeout = 0;
}
I have the same problem, but the app and all plugins still work fine after the error. It would be nice if the error was fixed though.
Same problem here, causing quite a few ANR's on android, with latest cordova android version 13.0.0
It appears this error only happens on API 33, on first start of the app.
API 34 works just fine, as far as I can tell, by running the same app in Android Studio emulators...
This should now be fixed in v17.0.0 of the plugin: on both Android & iOS, it now checks the plugin if is initialised (if the plugin JS API has been loaded by Cordova) before attempting to call the JS API. If not initialised when attempting to call the JS API from native, it queues up the calls and invokes them when the plugin has been initialised.
I just upgraded to Cordova@12, [email protected] and [email protected], targeting SDK 34 - and this issue is now happening upon app launch. Nothing else in my code base has changed, simply just upgrading several components has caused this error to happen. I know this was fixed in v17.0.0 - but it appears to be back.
Its not happening on every app launch though, its happening about 50% of the time.
@globules-io fix here looks like it did the trick: https://github.com/dpa99c/cordova-plugin-firebasex/pull/941 - restarted app about 20 times and the error hasn't triggered once.
@rolinger I think you mean https://github.com/dpa99c/cordova-plugin-firebasex/pull/942 right ?
@globules-io - I did mean #941, because that shows the actual config changes needed - I manually edited my files as opposed to doing a new pull.
@rolinger that won't work, you need #942