gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 00:05:14 28 4
gpt4 key购买 nike

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

No implementation found for method openActivity on channel 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">

主 Activity .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);
}
}

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

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

最佳答案

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

关于java - Flutter - 无法将 MethodChannel 与 Firebase onBackgroundMessage 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60514406/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com