Solvedflutterfire firebase-messaging: Android Integration

Android Integration
I'm not an android developer. I'm trying to use FCM with flutter
Using firebase_messaging: ^5.1.5

In android integrations, there is a section
Optionally handle background messages
Step 1 says Add an Application.java class to your app
I'm confused wherein the app.

  • inside the app folder
  • inside the app/main
  • inside android/

I tried adding it to all the places but my application crashes. No console errors but not able to run the application.

I have this exact code in my Application.java

package io.flutter.plugins.firebasemessagingexample;

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;

public class Application extends FlutterApplication implements PluginRegistrantCallback {
    @Override
    public void onCreate() {
        super.onCreate();
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    @Override
    public void registerWith(PluginRegistry registry) {
        GeneratedPluginRegistrant.registerWith(registry);
    }
}

also I have updated AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.venuemonk.agent_notify">

    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <application
        android:name=".Application"
        android:label="agent_notify"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            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">
            <!-- This keeps the window background of the activity showing
                 until Flutter renders its first frame. It can be removed if
                 there is no splash screen (such as the default splash screen
                 defined in @style/LaunchTheme). -->
            <meta-data
                android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                android:value="true" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Also, I get error's like firebasemessaging & firebasemessagingexample not resolved.

Improvements
If possible pls explicitly method where should be added.

Thank You in adv!.

40 Answers

✔️Accepted Answer

@OliverWangWei Sorry...

It was a mistake to edit the MainActivity.kr file.

I created a new Application.kr file and wrote it as follows:

package io.flutter.plugins.firebasemessagingexample;

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistry.PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()

        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    override fun registerWith(registry: PluginRegistry?) {
        GeneratedPluginRegistrant.registerWith(registry);
    }
}

Then proceed to Step 2 of Optionally handle background messages.

When I rewritten AndroidManifest.xml to run it from newly added Application.kr, it worked.

Other Answers:

I had similar issue and I had to write Application.kt like this:

Note: After this reinstall app on mobile device

package xxx.yyyyyyyy.app

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
class Application : FlutterApplication(), PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
        FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
    }
}

My app also crashed. I'm already following every step. Please update firebase_messaging example for this background implementation. Thank you :)

Logs

I/flutter (22742): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (22742): The following NoSuchMethodError was thrown attaching to the render tree:
I/flutter (22742): The method 'toRawHandle' was called on null.
I/flutter (22742): Receiver: null
I/flutter (22742): Tried calling: toRawHandle()
I/flutter (22742): 
I/flutter (22742): When the exception was thrown, this was the stack:
I/flutter (22742): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
I/flutter (22742): #1      FirebaseMessaging.configure (package:firebase_messaging/firebase_messaging.dart:122:55)

@trieuvi

toRawHandle' was called on null

This happens, if the isolate is not found.

You need an top-level function with the name "myBackgroundMessageHandler".
To do this, add the function in your main.dart:

 Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
   if (message.containsKey('data')) {
     // Handle data message
     final dynamic data = message['data'];
   }

   if (message.containsKey('notification')) {
     // Handle notification message
     final dynamic notification = message['notification'];
   }

   // Or do other work.
 }

`

@lynrin Thanks a lot, it works! However, I had an issue like #125 and #144 when I added the top level myBackgroundMessageHandler function in my code.

More Issues: