Nativescript-plugin-firebase: Android - Background notification causing app to crash after upgrade to 5.1.1

Created on 4 Jan 2018  路  5Comments  路  Source: EddyVerbruggen/nativescript-plugin-firebase

Hi,

I upgraded to the 5.1.1 of the plugin recently and am running tns 3.4
Notifications are received perfectly in the foreground.

However when running the app in the background on Android the notification is received in the phones notification panel. On clicking this background notification, the app crashes with the following error.

An uncaught Exception occurred on "main" thread.
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.nativescript.qbank/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: 
Calling js method onCreate failed

Error: com.google.firebase.database.DatabaseException: Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance.
    com.google.firebase.database.FirebaseDatabase.zzph(Unknown Source:41)
    com.google.firebase.database.FirebaseDatabase.setPersistenceEnabled(Unknown Source:3)
    com.tns.Runtime.callJSMethodNative(Native Method)
    com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
    com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
    com.tns.Runtime.callJSMethod(Runtime.java:957)
    com.tns.Runtime.callJSMethod(Runtime.java:941)
    com.tns.Runtime.callJSMethod(Runtime.java:933)
    com.tns.NativeScriptActivity.onCreate(NativeScriptActivity.java:13)
    android.app.Activity.performCreate(Activity.java:6998)
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1230)
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2899)
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3046)
    android.app.ActivityThread.-wrap11(Unknown Source:0)
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1688)
    android.os.Handler.dispatchMessage(Handler.java:105)
    android.os.Looper.loop(Looper.java:164)
    android.app.ActivityThread.main(ActivityThread.java:6809)
    java.lang.reflect.Method.invoke(Native Method)
    com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
File: "file:///data/data/org.nativescript.qbank/files/app/tns_modules/nativescript-plugin-firebase/firebase.js, line: 189, column: 44

StackTrace: 
    Frame: function:'runInit', file:'file:///data/data/org.nativescript.qbank/files/app/tns_modules/nativescript-plugin-firebase/firebase.js', line: 189, column: 45
    Frame: function:'Observable.notify', file:'file:///data/data/org.nativescript.qbank/files/app/tns_modules/tns-core-modules/data/observable/observable.js', line: 103, column: 23
    Frame: function:'ActivityCallbacksImplementation.notifyLaunch', file:'file:///data/data/org.nativescript.qbank/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 621, column: 21
    Frame: function:'ActivityCallbacksImplementation.onCreate', file:'file:///data/data/org.nativescript.qbank/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 585, column: 29
    Frame: function:'NativeScriptActivity.onCreate', file:'file:///data/data/org.nativescript.qbank/files/app/tns_modules/tns-core-modules/ui/frame/activity.js', line: 20, column: 25


    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3046)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1688)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6809)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: com.tns.NativeScriptException: 
Calling js method onCreate failed

Error: com.google.firebase.database.DatabaseException: Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance.
    com.google.firebase.database.FirebaseDatabase.zzph(Unknown Source:41)
    com.google.firebase.database.FirebaseDatabase.setPersistenceEnabled(Unknown Source:3)
    com.tns.Runtime.callJSMethodNative(Native Method)
    com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
    com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
    com.tns.Runtime.callJSMethod(Runtime.java:957)
    com.tns.Runtime.callJSMethod(Runtime.java:941)
    com.tns.Runtime.callJSMethod(Runtime.java:933)
    com.tns.NativeScriptActivity.onCreate(NativeScriptActivity.java:13)
    android.app.Activity.performCreate(Activity.java:6998)
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1230)
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2899)
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3046)
    android.app.ActivityThread.-wrap11(Unknown Source:0)
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1688)
    android.os.Handler.dispatchMessage(Handler.java:105)
    android.os.Looper.loop(Looper.java:164)
    android.app.ActivityThread.main(ActivityThread.java:6809)
    java.lang.reflect.Method.invoke(Native Method)
    com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
File: "file:///data/data/org.nativescript.qbank/files/app/tns_modules/nativescript-plugin-firebase/firebase.js, line: 189, column: 44

StackTrace: 
    Frame: function:'runInit', file:'file:///data/data/org.nativescript.qbank/files/app/tns_modules/nativescript-plugin-firebase/firebase.js', line: 189, column: 45
    Frame: function:'Observable.notify', file:'file:///data/data/org.nativescript.qbank/files/app/tns_modules/tns-core-modules/data/observable/observable.js', line: 103, column: 23
    Frame: function:'ActivityCallbacksImplementation.notifyLaunch', file:'file:///data/data/org.nativescript.qbank/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 621, column: 21
    Frame: function:'ActivityCallbacksImplementation.onCreate', file:'file:///data/data/org.nativescript.qbank/files/app/tns_modules/tns-core-modules/ui/frame/frame.js', line: 585, column: 29
    Frame: function:'NativeScriptActivity.onCreate', file:'file:///data/data/org.nativescript.qbank/files/app/tns_modules/tns-core-modules/ui/frame/activity.js', line: 20, column: 25


    at com.tns.Runtime.callJSMethodNative(Native Method)
    at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1088)
    at com.tns.Runtime.callJSMethodImpl(Runtime.java:970)
    at com.tns.Runtime.callJSMethod(Runtime.java:957)
    at com.tns.Runtime.callJSMethod(Runtime.java:941)
    at com.tns.Runtime.callJSMethod(Runtime.java:933)
    at com.tns.NativeScriptActivity.onCreate(NativeScriptActivity.java:13)
    at android.app.Activity.performCreate(Activity.java:6998)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1230)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2899)
    ... 9 more
Caused by: com.google.firebase.database.DatabaseException: Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance.
    at com.google.firebase.database.FirebaseDatabase.zzph(Unknown Source:41)
    at com.google.firebase.database.FirebaseDatabase.setPersistenceEnabled(Unknown Source:3)
    ... 19 more

Please suggest what I can do to fix this issue.

Android bug

All 5 comments

If the app is open and the device back button is pressed to minimize the app - after reopenning the app the above error appears only if firebase is initialized with persist: true.

Yes that is correct for my case.
Persist is true
The app is running in the background (not just through back but also by pressing home fro instance).
On receiving a firebase notification, the notification correctly appears in the notification panel of the phone. On clicking this notification the app opens and fails with the error outlined in the issue above.

Pls help me with this issue.

Looks like the app invokes firebase.init() again after returning from the background. init() calls setPersistenceEnabled if that's configured.

If you guys can confirm that's the case then try not calling init() a second time during the app's lifetime, and I'll see how I can prevent this error when you inadvertently do.

Hi @EddyVerbruggen ,

You are correct. That's what seems to be happening. I tried to prevent firebase.init from happening again as below, but that didn't work for me. Is there any other way to check and prevent init from calling a second time?

if (firebase.ServerValue !== undefined) { console.log("Firebase already there"); } else{ firebase.init({.......

I hope 5.1.2 will help in this respect.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dopefatimah picture dopefatimah  路  3Comments

tsili852 picture tsili852  路  3Comments

b02505048 picture b02505048  路  3Comments

EddyVerbruggen picture EddyVerbruggen  路  3Comments

SBD580 picture SBD580  路  3Comments