Flutter – 在使用MethodChannel与Firebase的onBackgroundMessage时遇到了问题。

huangapple 未分类评论63阅读模式
标题翻译

Flutter - Failed to use MethodChannel with Firebase onBackgroundMessage

问题

我在尝试打开一个 Android 活动时遇到了问题,而应用程序处于关闭状态。请查看下面的代码,在从 Firebase 接收到数据的通知时,当应用程序在后台运行时,我应该使用 MethodChannel 打开一个活动以访问 Java,但是我收到了以下错误消息:

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

我哪里出错了,如何使其工作?

英文翻译

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(&quot;io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin&quot;));
    }
}

AndroidManifest.xml

&lt;application
        android:name=&quot;com.example.mobile.Application&quot;
        android:label=&quot;mobile&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;&gt;

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 = &quot;com.example.service/start&quot;;

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

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

    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&lt;dynamic&gt; myBackgroundMessageHandler(Map&lt;String, dynamic&gt; message) async {
  MethodChannel channel = new MethodChannel(&quot;com.example.service/start&quot;);

  if (message.containsKey(&#39;data&#39;)) {
    final dynamic data = message[&#39;data&#39;];

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

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

答案1

得分: 0

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

英文翻译

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

huangapple
  • 本文由 发表于 2020年3月4日 03:42:23
  • 转载请务必保留本文链接:https://java.coder-hub.com/60514406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定