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

[firebase_messaging] Android v2 embedding crashes #1754

Closed
cranst0n opened this issue Jan 2, 2020 · 96 comments
Closed

[firebase_messaging] Android v2 embedding crashes #1754

cranst0n opened this issue Jan 2, 2020 · 96 comments
Labels
impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) plugin: messaging type: bug Something isn't working type: documentation Improvements or additions to documentation

Comments

@cranst0n
Copy link

cranst0n commented Jan 2, 2020

Describe the bug
Since updating my app to the Android v2 embedding I've been getting reports in Crashlytics concerning ensureInitializationComplete must be called after startInitialization io.flutter.embedding.engine.loader.FlutterLoader.ensureInitializationComplete.

The Flutter 1.12 upgrade guide says to remove all references to the startInitialization or ensureInitializationComplete methods but I see 2 references to these methods: here and here.

To Reproduce
Steps to reproduce the behavior:

  1. Upgrade app to v2 embedding.
  2. Start app and then close it.
  3. Send notification from the Firebase Notification Composer.
  4. Notice app crash notification on device.
  5. See error or incorrect behavior

I have not seen the issue when I leave the app open and send the notification.

Expected behavior
Notification displayed normally while app is closed.

Additional context
Here is what the Crashlytics report looks like:

Fatal Exception: java.lang.RuntimeException
Unable to create service io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService: java.lang.IllegalStateException: ensureInitializationComplete must be called after startInitialization
android.app.ActivityThread.handleCreateService (ActivityThread.java:3746)
android.app.ActivityThread.access$1400 (ActivityThread.java:235)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1784)
android.os.Handler.dispatchMessage (Handler.java:106)
android.os.Looper.loop (Looper.java:214)
android.app.ActivityThread.main (ActivityThread.java:6986)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:494)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1445)

Caused by java.lang.IllegalStateException
ensureInitializationComplete must be called after startInitialization
io.flutter.embedding.engine.loader.FlutterLoader.ensureInitializationComplete (FlutterLoader.java:153)
io.flutter.view.FlutterMain.ensureInitializationComplete (FlutterMain.java:80)
io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.onCreate (FlutterFirebaseMessagingService.java:71)
android.app.ActivityThread.handleCreateService (ActivityThread.java:3734)
android.app.ActivityThread.access$1400 (ActivityThread.java:235)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1784)
android.os.Handler.dispatchMessage (Handler.java:106)
android.os.Looper.loop (Looper.java:214)
android.app.ActivityThread.main (ActivityThread.java:6986)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:494)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1445)
@cranst0n cranst0n added the type: bug Something isn't working label Jan 2, 2020
@iapicca iapicca added the type: documentation Improvements or additions to documentation label Jan 3, 2020
@cranst0n
Copy link
Author

Closing this. Seems that I had left android:name="io.flutter.app.FlutterApplication" out of my <application> definition in AndroidManifest.xml.

@philipgiuliani
Copy link

I have the exact same issue. The app crashes when I receive a notification while the app is closed, but I get the notification when the app is running. I migrated the app using the upgrade guide.

I tried adding android:name="io.flutter.app.FlutterApplication" and it didnt help. The guide actually tells you to remove it.

@carmas123
Copy link

Please reopen the issue because the problem still remain

@W2YAdmin
Copy link

W2YAdmin commented Feb 2, 2020

Please reopen the issue I am facing the same thing as well!

@cranst0n cranst0n reopened this Feb 2, 2020
@cranst0n
Copy link
Author

cranst0n commented Feb 2, 2020

Reopened. Should not have closed it.

@DavidKuennen
Copy link

Anyone with a solution for this? Having the same problem.

@barees63
Copy link

I'm seeing this also

@davcrash
Copy link

I have the same problem

@ryanheise
Copy link

The bug happens because the plugin still calling FlutterMain.ensureInitializationComplete which it shouldn't do with the new v2 embedding. The 1.12 migration guide says:

If you invoke FlutterMain.startInitialization(...) or FlutterMain.ensureInitializationComplete(...) anywhere in your code, you should remove those calls. Flutter now initializes itself at the appropriate time.

So the "ensureInitializationComplete must be called after startInitialization" error happens because the plugin is calling ensureInitializedComplete when under v2, startInitialization is never called.

Until this is fixed, you can get around the error by making your app use the old io.flutter.app.FlutterApplication class as your application class, or if you have your own custom application class, you can either make it extend FlutterApplication or you can just call startInitialization in your own application class's onStart (which is what FlutterApplication basically does.)

@miniskulljob
Copy link

I'm having this issue too, unfortunately I can't go back and revert the v2 embedding implementation in my app because other plugins depend on it to work properly.

Any chance to fix this soon? This issue is quite old now...

@ivanchaukn
Copy link

I'm having the same issue too! Can we get it fixed soon?

@ryanbliss
Copy link

Having the same issue and it is impacting a large percentage of our users. Would appreciate a speedy fix :)

@mnorhamizan
Copy link

I'm having this issue as well

@ryanheise
Copy link

You may as well try the workaround I described in my post above. Just to clarify, this workaround does NOT require you to revert your project to pre-v2.

@mnorhamizan
Copy link

@ryanheise How about those in GeneratedPluginRegistrants? Some of my package use those do I need to include that in MainActivity.kt?

@helenaford helenaford added impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) plugin: messaging labels Apr 20, 2020
@Boehrsi
Copy link

Boehrsi commented Apr 27, 2020

Is there any information in regards to a release with a fix for this issues? Right now it's crashing always, as soon as the app isn't running at all (so e.g. killed by the system or swiped out of the app switcher). So the user will get crash dialogs over and over, for every push message he receives. This is something highly critical and would block any production release of an app (IMHO).
Workarounds are something we can do during testing etc., but as I'm developing a quite complex app, with different native interactions, it would be great to have a stable backbone for the push logic and setup.

Btw. thanks for the nice plugins and all the efforts integrating Flutter into the existing Firebase world. Looking forward to more nice Flutter integrations. 👍

@Boehrsi
Copy link

Boehrsi commented Apr 29, 2020

Issues related to this problem (it seems that general parts are missing for the Android v2 embedding: setup call adjustments, documentation and some internal handling e.g. for plugin calls in the onBackgroundMessage method):

Only did a really short search, so probably there are more related issues. Just wanted to show that this problem is probably affecting a larger number of developers and users than visible in this particular ticket.

@MichaelM97
Copy link

Why is this not labelled severe? This causes crash dialogs to show periodically when the application is closed, which would cause most users to either disable permissions or just delete the app. This surely should be made a priority, considering that as more and more apps/packages make the move to the v2 embedding this will affect every Android user.

@themisir
Copy link

themisir commented May 4, 2020

I don't know how but I solved it mysteriously. 🧐

@robert-virkus
Copy link

#2510 also seems to be related. Please - PRETTY PLEASE make this is a priority.

@robert-virkus
Copy link

#2468 also seems to be related.

@mootw
Copy link

mootw commented May 14, 2020

I was able to fix this issue when pressing on notifications by doing this, but please read the entire post before continuing.

This is what fixed the issue for me:
Add this to the application tag: android:name="io.flutter.app.FlutterApplication"

Here is my application tag

<application
        android:name="io.flutter.app.FlutterApplication"
        tools:replace="android:label"
        android:label="App Name"
        android:icon="@mipmap/ic_launcher">

My primary activity for the app is this

<activity
            android:name="io.flutter.embedding.android.FlutterActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"

            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">

            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <!--FIREBASE CLOUD MESSAGING-->
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

I also included these tags under application, among other various widgets.

<meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />

I sent a notification through a notification channel "test_channel" using the firebase console and the app did not crash when I pressed the notification; it properly entered the app. I did not test if data was received properly, but I would assume it works because it no longer crashes.

https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects
This upgrade guide indicates to remove android:name="io.flutter.app.FlutterApplication" from the application tag. Adding it back in seems to fix the firebase bug, but it seems like it still functions fine with it added back in, even using v2 embedding. It seems to be working fine with all of my V2 plugins and background_fetch also is working fine as far as I can tell. This could be a temporary fix until the issue is addressed properly?

Please do test it out for yourself because this goes against the update guidelines which tell you to remove this.

@BrunoEleodoro
Copy link

make sense... let's wait for Elliot input on this.

@vanlooverenkoen
Copy link

vanlooverenkoen commented Oct 26, 2020

Replacing this is the fix that I will use for now:

Creating custom Application that extends Flutter Application & adding it to the AndroidManifest.xml

Changing the firebase_messaging dependency to:

  firebase_messaging:
    git:
      url: git://github.com/mondoktamas/flutterfire
      branch: support-android-v2-embedding
      path: packages/firebase_messaging

=>>> This wasn't working for iOS 😑

@Ehesp
Copy link
Member

Ehesp commented Oct 26, 2020

Yeah we've removed a few things (specifically configure) since it was too easy to cause unwanted side effects from simply calling it more than once etc.

We're going to push another release soon with some more changes. Note; the Firebase SDKs need updating in version so we've also got to bump all other package versions... 😅

@kinex
Copy link

kinex commented Oct 27, 2020

If you want to try 8.0.0-dev.1 now, you need to put this to app/build.gradle or the build fails:

rootProject.ext {
  set('FlutterFire', [
    FirebaseSDKVersion: '25.12.0'
  ])
}

Not sure if that is fully safe regarding other FlutterFire plugins, but I haven't noticed new issues so far.

@vanlooverenkoen
Copy link

vanlooverenkoen commented Oct 27, 2020

I can't use this plugin with v2 embedding every "solution" in this post did not work for me.

I will revert to pre v2 embedding 😢

@Ehesp are there dates when the next dev en stable release will be released?

We will release in a couple of weeks. Is that feasible?

@Ehesp
Copy link
Member

Ehesp commented Oct 27, 2020

@vanlooverenkoen In the next few days is our plan. We've just got to ensure the SDK upgrades work with everything else, and also it's the Firebase Summit this week so want to make sure there's nothing new landing which will break things :)

@vanlooverenkoen
Copy link

@vanlooverenkoen In the next few days is our plan. We've just got to ensure the SDK upgrades work with everything else, and also it's the Firebase Summit this week so want to make sure there's nothing new landing which will break things :)

@Ehesp the next few days is for a dev release? or a stable release?

@signshah
Copy link

Hope to see the fix soon... my clients are complaining that app is crashing whenever they receives notification :-(

@vanlooverenkoen
Copy link

Hope to see the fix soon... my clients are complaining that app is crashing whenever they receives notification :-(

Crashes can be fixed by replacing the Application with a custom one that extends FlutterApplication. If you want to receive notifications in the background. revert to pre v2 embedding

@ReniDelonzek
Copy link

Adding

   <application
        android:name="io.flutter.app.FlutterApplication"

Fixes crashes provisionally. But that shouldn't be the final solution

@signshah
Copy link

<application
android:name="io.flutter.app.FlutterApplication"

When I put the above line, it stopped crashing... I hope there will not be any side effect of it... Thanks for your time and help @ReniDelonzek

@jlubeck
Copy link

jlubeck commented Oct 28, 2020

Adding

   <application
        android:name="io.flutter.app.FlutterApplication"

Fixes crashes provisionally. But that shouldn't be the final solution

I have this in my Manifest and I'm still getting crashes whenever I receive a notification....

java.lang.RuntimeException: Unable to create service io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService: java.lang.RuntimeException: PluginRegistrantCallback is not set

I'm not even using the onBakgroundMessage callback.

By the way, if I remove this from the manifest it stops crashing:

            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

But of course tapping on the notifications does nothing now...

@vanlooverenkoen
Copy link

@jdeltoft
Copy link

@vanlooverenkoen any idea if that fixes this? All I see is:

  • FEAT: bump firebase sdk version to 6.33.0.
  • DOCS: typos.

@vanlooverenkoen
Copy link

Not sure I will try to migrate in a couple hours. Still working on something else first.

@jdeltoft
Copy link

Sorry, maybe a dumb question but I wanted to test this out ASAP so figured I would ask. I had everything working well, with firebase messages triggered my app updates etc. I had this very random crash though which led me to this thread after getting the bugreport indication about "ensureInitializationComplete must be called after startInitialization".

So I just went from version "^7.0.3" to version "8.0.0-dev2". Then I can't compile because something is deprecated I think with FirebaseMessaging. I used to have this:

FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

but then the _firebaseMessaging didn't have "configure" or "onIosSettingsRegistered" defined anymore?

I see this was deprecated to:

FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;

but I still can't compile with configure(). In the example it still uses deprecated version I had initially so I'm just at a loss I think with DART syntax I should be using. Any help would be appreciated!

@vanlooverenkoen
Copy link

You can use FirebaseMessaging.onMessage or something like that to get the old methods

@Ehesp
Copy link
Member

Ehesp commented Oct 30, 2020

Migration guide & docs will be released soon, however the dev release is good to start using (+ testing).

Aside from some local tests failing, you can find everything here: https://github.com/invertase/flutterfire/pull/76

@jdeltoft
Copy link

Ok, this will take me some time to setup for migration and get the new way of using firebase setup. I'll do that but in the short term if I add the line above:

    <!-- temp change per https://github.com/FirebaseExtended/flutterfire/issues/1754 -->
    android:name="io.flutter.app.FlutterApplication"

in my AndroidManifest.xml in the I have two questions.

  1. does this also solve the issue on iOS? I'm showing my Flutter lack of knowledge here, but I see this random crash on ios and android and I'm assuming they are the same (both random and frequent when app is in background). But I would also guess Android.Manifest.xml has nothing to do with my app on iOS? Is that true?

  2. I thought I realized a way to trigger this background crash now. Send my app messages when it's in the background. In my Flutter app these happen based on changes to something like a slider level on the web. I move the slider in my app running as a website and that triggers FCM to update all apps for that same client that's using the web. I validate this works when in the foreground, then put my app in the background and trigger the same thing but I don't see a crash. Any idea?

@Ehesp
Copy link
Member

Ehesp commented Oct 30, 2020

@jdeltoft

  1. That has no impact on anything to do with iOS.
  2. Hard to say, the rework is a full overhaul, brand new code and implementation of FCM so hopefully your issues are fixed.

@joniaiuser
Copy link

joniaiuser commented Oct 30, 2020

@Ehesp Has the data field been tested on iOS? After migrating to 8.0.0-dev2, my iOS notifications are now breaking when testing on iPhone X, iOS 14.1 with profile mode.. I followed the documentation, here's a code snippet.

    var initialMessage = await firebaseMessaging.getInitialMessage();

    if (initialMessage?.data['type'] == 'inbox') {
      await goToInbox();
    }

    if (initialMessage?.data['type'] == 'qna') {
      await goToQna(initialMessage?.data);
    }

    onMessage = FirebaseMessaging.onMessageOpenedApp
        .listen((RemoteMessage message) async {
      if (message.data['type'] == 'inbox') {
        await goToInbox();
      }
      if (message.data['type'] == 'qna') {
        await goToQna(message.data);
      }
    });

The previous version when I used .configure() was working perfectly for iOS.

Specifically what happens now is that I receive the notification successfully but upon tapping on it, it opens the app but crashes it immediately.

@joniaiuser
Copy link

In the previous version, the data field behaved differently for iOS and Android, where in iOS the data field was missing and the data was combined into the top-level in message instead. Hence in that case I had to catch both scenarios separately.. I'm assuming the new updated plugin is doing that behind-the-scenes? I tried to catch it by casting message to a Map<String,String> when data is null, but that doesn't seem to work for me either.

@Salakar
Copy link
Member

Salakar commented Nov 5, 2020

Hey all 👋

As part of our roadmap (#2582) we've just shipped a complete rework of the firebase_messaging plugin that aims to solve this and many other issues.

If you can, please try out the dev release (see the migration guide for upgrading and for changes) and if you have any feedback then join in the discussion here.

Given the scope of the rework I'm going to go ahead and close this issue in favor of trying out the latest plugin.

Thanks everyone.

@Salakar Salakar closed this as completed Nov 5, 2020
@shaiwals
Copy link

shaiwals commented Nov 5, 2020

When will this fix get to a stable branch?

@narayanansampath
Copy link

narayanansampath commented Nov 17, 2020

Adding

   <application
        android:name="io.flutter.app.FlutterApplication"

Fixes crashes provisionally. But that shouldn't be the final solution

I have this in my Manifest and I'm still getting crashes whenever I receive a notification....

java.lang.RuntimeException: Unable to create service io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService: java.lang.RuntimeException: PluginRegistrantCallback is not set

I'm not even using the onBakgroundMessage callback.

By the way, if I remove this from the manifest it stops crashing:

            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

But of course tapping on the notifications does nothing now...

New migration guide says that it'll open the app after clicking notification even after removing these

Messaging only: If you added the previous versions intent-filter to your android/app/src/main/AndroidManifest.xml file, remove it:

<intent-filter> <action android:name="FLUTTER_NOTIFICATION_CLICK" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>

doc link: https://firebase.flutter.dev/docs/migration/

We don't need to create Application.kt or include anything in AndroidManifest

@leokvncrz
Copy link

Adding

   <application
        android:name="io.flutter.app.FlutterApplication"

Fixes crashes provisionally. But that shouldn't be the final solution

This one works for me.

@barapa
Copy link

barapa commented Nov 30, 2020

I removed the intent-filter as specified in the migration doc, but now clicking on a notification no longer brings the user into the app.

<intent-filter> <action android:name="FLUTTER_NOTIFICATION_CLICK" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>

@firebase firebase locked and limited conversation to collaborators Dec 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) plugin: messaging type: bug Something isn't working type: documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests