Flutter - не удалось использовать MethodChannel с Firebase onBackgroundMessage - PullRequest
0 голосов
/ 03 марта 2020

Я получаю ошибку при попытке открыть действие android, когда приложение закрыто. Из приведенного ниже кода видно, что когда я получаю уведомление о данных из firebase, когда приложение находится в фоновом режиме, я должен открыть действие, используя MethodChannel для доступа к java, но я получаю эту ошибку:

Не найдена реализация метода openActivity на канале com.example.service / start

Приложение. 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");
  }
}

Где я иду не так, и как я могу заставить его работать?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...