AppsFlyerSDK / appsflyer-flutter-plugin

Flutter Plugin for AppsFlyer SDK
MIT License
144 stars 114 forks source link

Init AppsFlyer in a background process on Android device causes a NullPointerException #109

Closed Luvti closed 3 years ago

Luvti commented 3 years ago

Describe the bug Init AppsFlyer in a background process on Android device causes a NullPointerException

To Reproduce Steps to reproduce the behavior:

  1. Use Firebase messaging for sending a push notification in the background process FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);
  2. init AppsFlyer in background process (backgroundMessageHandler):
    appsflyerSdk = AppsflyerSdk(appsFlyerOptions);
    await appsflyerSdk.initSdk(
    registerConversionDataCallback: true,
    registerOnAppOpenAttributionCallback: true,
    registerOnDeepLinkingCallback: true,
    );
  3. Error:
    E/MethodChannel#af-api: Failed to handle method call
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
        at com.appsflyer.AppsFlyerLibCore.start(:921)
        at com.appsflyer.AppsFlyerLibCore.start(:902)
        at com.appsflyer.AppsFlyerLibCore.start(:897)
        at com.appsflyer.appsflyersdk.AppsflyerSdkPlugin.initSdk(AppsflyerSdkPlugin.java:586)
        at com.appsflyer.appsflyersdk.AppsflyerSdkPlugin.onMethodCall(AppsflyerSdkPlugin.java:128)
        at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
        at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
        at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:818)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:336)
        at android.os.Looper.loop(Looper.java:197)
        at android.app.ActivityThread.main(ActivityThread.java:8016)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076)

Expected behavior No error.

GM-appsflyer commented 3 years ago

Hey @Luvti , Thanks for reaching out!

I've investigated a bit, and it seems because "_firebaseMessagingBackgroundHandler' runs in its own isolate outside your applications context" this is probably the reason this error appears. Please check this section on the firebase flutter docs: https://firebase.flutter.dev/docs/messaging/usage/#background-messages

What is your use case? Maybe we can find another way to tackle this.

SkywalkerDarren commented 3 years ago

I got the same issue when the application was in the background and then flutter activity was destroyed. Turn on 'Don't keep activities' in 'Developer options' can reproduce this.

E/MethodChannel#af-api: Failed to handle method call
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
        at com.appsflyer.AppsFlyerLibCore.start(:921)
        at com.appsflyer.AppsFlyerLibCore.start(:902)
        at com.appsflyer.AppsFlyerLibCore.start(:897)
        at com.appsflyer.appsflyersdk.AppsflyerSdkPlugin.initSdk(AppsflyerSdkPlugin.java:586)
        at com.appsflyer.appsflyersdk.AppsflyerSdkPlugin.onMethodCall(AppsflyerSdkPlugin.java:128)
        at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
        at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
        at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:818)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:335)
        at android.os.Looper.loop(Looper.java:183)
        at android.app.ActivityThread.main(ActivityThread.java:7664)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
felipe-xavier commented 1 year ago

Hey, has anyone found a solution for this?