MissingPluginException(在通道plugins.flutter.io/firebase_messaging上找不到方法getToken的实现)

Tam*_*ury 3 dart google-play firebase flutter firebase-cloud-messaging

在我的 flutter android 应用程序的发布版本上,我收到此错误。

我努力了:

从 Firebase 中的 Google Play 控制台更新 SHA-1 密钥 更新 google-services.json 重建和推出。

我仍然收到此错误。我启用了 INTERNET_PERMISSIONS 并且也启用了cleartext=true。

应用程序在 iOS 和 Android 调试模式下也能正常工作。

2021-02-15 00:47:37.450 2888-2932/? E/flutter:[错误:flutter/lib/ui/ui_dart_state.cc(177)] 未处理的异常:MissingPluginException(在通道plugins.flutter.io/firebase_messaging上找不到方法配置的实现)#0 MethodChannel._invokeMethod(包:flutter) /src/services/platform_channel.dart:157) 2021-02-15 00:47:37.450 2888-2932/? E/flutter:[错误:flutter/lib/ui/ui_dart_state.cc(177)]未处理的异常:MissingPluginException(在通道plugins.flutter.io/firebase_messaging上找不到方法getToken的实现)#0 MethodChannel._invokeMethod(包:flutter) /src/services/platform_channel.dart:157) #1 FirebaseMessaging.getToken (包:firebase_messaging/firebase_messaging.dart:146)

Pri*_*esh 7

请确保您已执行以下步骤:

1:在pubspecs.yaml中添加dep

firebase_core: // latest version
firebase_messaging: // latest version
Run Code Online (Sandbox Code Playgroud)

2:在应用程序文件中添加依赖和服务build.gradle

dependencies {

 implementation platform('com.google.firebase:firebase-bom:26.5.0')
 
 implementation 'com.google.firebase:firebase-messaging'
 
}

apply plugin: 'com.google.gms.google-services'
Run Code Online (Sandbox Code Playgroud)

3:仅在目录下正确的google-service.jsonapp

4:初始化于main.dart

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}
Run Code Online (Sandbox Code Playgroud)

5:将此代码添加到您的主类或第一个执行类中

FirebaseMessaging.instance.getToken().then((token){
  print("token $token");
}); 
Run Code Online (Sandbox Code Playgroud)