Flutter - Failed to use MethodChannel with Firebase onBackgroundMessage

Viewed 452

I am getting a failure when trying to open an android activity while the application is closed. See in the code below that, when I receive a notification of data from firebase, while the app is in the background, I should open an activity using MethodChannel to access java, but I get this error:

No implementation found for method openActivity on channel com.example.service/start

Application.java

package com.example.mobile;

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;

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

    @Override
    public void registerWith(PluginRegistry registry) {
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }
}

AndroidManifest.xml

<application
        android:name="com.example.mobile.Application"
        android:label="mobile"
        android:icon="@mipmap/ic_launcher">

MainActivity.java

package com.example.mobile;

import androidx.annotation.NonNull;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;

public class MainActivity extends FlutterActivity {
    private static final String CHANNEL = "com.example.service/start";

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);

        new MethodChannel(flutterEngine.getDartExecutor(), CHANNEL)
                .setMethodCallHandler(
                        (call, result) -> {
                            if(call.method.equals("openActivity")){
                                openActivity();
                                result.success("open activity");
                            }
                        }
                );
    }

    void openActivity(){
       Intent i = new Intent(this, SecondActivity.class);
       startActivity(i);
    }
}

main.dart

_firebaseMessaging.configure(
      onMessage: (message) async {
        //
      },
      onLaunch: (message) {
        //
      },
      onResume: (message) {
        //
      },
      onBackgroundMessage: myBackgroundMessageHandler,
    );

Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
  MethodChannel channel = new MethodChannel("com.example.service/start");

  if (message.containsKey('data')) {
    final dynamic data = message['data'];

    var open = await channel.invokeMethod("openActivity");
  }
}

Where am I going wrong, and how can I make it work?

1 Answers

In your AndroidManifest.xml file the android:name must be android:name=".Application", And make sure that MainActivity.java and Application.java are in same folder

Related