当我收到 Firebase 消息 (FCM) 时,我想在 Android 中启动一些本机代码,但收到此错误:
E/flutter (16352): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: MissingPluginException(No implementation found for method wakeUpApp on channel com.diesetelecom.smartdiese.androidChannel)
E/flutter (16352): #0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:332:7)
E/flutter (16352): <asynchronous suspension>
我已经尝试卸载应用程序 -> flutter clean -> flutter pub get -> flutter run 但没有成功
这是我的 main.dart 中 flutter 中的代码,我尝试删除“com.diesetelecom.smartdiese”以保留“androidChannel”,但我仍然遇到错误。
@pragma('vm:entry-point')
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
...
const MethodChannel androidChannel = MethodChannel('com.diesetelecom.smartdiese.androidChannel');
androidChannel.invokeMethod("wakeUpApp");
}
Future<void> main() async {
...
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
...
await AssetsManager.initAssets();
await dotenv.load();
runApp(const App());
}
这是我的 MainActivity.kt,知道当我尝试从 firebaseMessagingBackgroundHandler 调用 Android 方法时出现此错误,但它在外部工作
package com.diesetelecom.smart_diese
import android.content.Context
import android.content.Intent
import android.media.AudioManager
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import android.util.Log
class MainActivity: FlutterActivity() {
private val CHANNEL = "androidChannel"
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "switchAudioOutput") {
val audioManager: AudioManager = getApplicationContext().getSystemService(Context.AUDIO_SERVICE) as AudioManager
audioManager.setMode(AudioManager.MODE_IN_CALL)
audioManager.setSpeakerphoneOn(!audioManager.isSpeakerphoneOn)
result.success(audioManager.isSpeakerphoneOn)
} else if(call.method == "wakeUpApp") {
Log.d("ActivityMain", "This is a small test text")
result.success(true)
} else {
result.notImplemented()
}
}
}
}
我注意到您的
MainActivity.kt
文件中存在问题。您已经创建了一个名为 CHANNEL
的顶级变量 private val CHANNEL = "androidChannel"
。
请记住,您的方法通道名称应在两个平台中同步,即在
Flutter
和 Kotlin
或 Swift
中。您尝试从 wakeUpApp
平台调用 Flutter
方法,方法通道名称为 com.diesetelecom.smartdiese.androidChannel
,而在 Kotlin
中,没有具有此名称的方法通道,因为您在 android 中将通道名称设置为 androidChannel
这就是为什么没有针对您在 Flutter
代码中指定的名称的实现。
您应该尝试以下代码才能使其运行:
颤动面:
@pragma('vm:entry-point')
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
...
const MethodChannel androidChannel = MethodChannel('com.diesetelecom.smartdiese.androidChannel');
androidChannel.invokeMethod("wakeUpApp");
}
Future<void> main() async {
...
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
...
await AssetsManager.initAssets();
await dotenv.load();
runApp(const App());
}
Kotlin 方面:
package com.diesetelecom.smart_diese
import android.content.Context
import android.content.Intent
import android.media.AudioManager
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import android.util.Log
class MainActivity: FlutterActivity() {
private val CHANNEL = "com.diesetelecom.smartdiese.androidChannel"
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "switchAudioOutput") {
val audioManager: AudioManager = getApplicationContext().getSystemService(Context.AUDIO_SERVICE) as AudioManager
audioManager.setMode(AudioManager.MODE_IN_CALL)
audioManager.setSpeakerphoneOn(!audioManager.isSpeakerphoneOn)
result.success(audioManager.isSpeakerphoneOn)
} else if(call.method == "wakeUpApp") {
Log.d("ActivityMain", "This is a small test text")
result.success(true)
} else {
result.notImplemented()
}
}
}
}