Flutter - 无法将 MethodChannel 与 Firebase onBackgroundMessage 一起使用

Posted

技术标签:

【中文标题】Flutter - 无法将 MethodChannel 与 Firebase onBackgroundMessage 一起使用【英文标题】:Flutter - Failed to use MethodChannel with Firebase onBackgroundMessage 【发布时间】:2020-06-16 06:11:18 【问题描述】:

在应用程序关闭时尝试打开 android 活动时失败。在下面的代码中看到,当我收到来自 firebase 的数据通知时,当应用程序在后台时,我应该使用 MethodChannel 打开一个活动来访问 java,但我收到此错误:

在通道 com.example.service/start 上找不到方法 openActivity 的实现

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");
  

我哪里出错了,我怎样才能让它发挥作用?

【问题讨论】:

你解决了这个问题吗?我也有同样的问题。谢谢 【参考方案1】:

在您的 AndroidManifest.xml 文件中,android:name 必须是 android:name=".Application",并确保 MainActivity.java 和 Application.java 在同一个文件夹中

【讨论】:

以上是关于Flutter - 无法将 MethodChannel 与 Firebase onBackgroundMessage 一起使用的主要内容,如果未能解决你的问题,请参考以下文章

无法将flutter现有项目更改为flutter web

Flutter - 无法将文件复制到新路径

Flutter 无法将频道切换到 beta

无法将 Flutter 项目迁移到 AndroidX

Flutter:无法将 ListView 添加到示例应用程序

Firebase/Flutter - 无法将数据写入数据库