我已经将我的flutter应用程序与firebase消息传递集成,并成功收到通知,所有工作正常,但现在当我试图再次运行该项目时,它显示了错误,当我向设备发送通知时,它停止了。
误差
D/AndroidRuntime( 8359): Shutting down VM
E/AndroidRuntime( 8359): FATAL EXCEPTION: main
E/AndroidRuntime( 8359): Process: com.tapfy.me, PID: 8359
E/AndroidRuntime( 8359): java.lang.RuntimeException: Unable to create service io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService: java.lang.RuntimeException: PluginRegistrantCallback is not set.
E/AndroidRuntime( 8359): at android.app.ActivityThread.handleCreateService(ActivityThread.java:4204)
E/AndroidRuntime( 8359): at android.app.ActivityThread.access$1500(ActivityThread.java:237)
E/AndroidRuntime( 8359): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
E/AndroidRuntime( 8359): at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime( 8359): at android.os.Looper.loop(Looper.java:223)
E/AndroidRuntime( 8359): at android.app.ActivityThread.main(ActivityThread.java:7656)
E/AndroidRuntime( 8359): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 8359): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/AndroidRuntime( 8359): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
E/AndroidRuntime( 8359): Caused by: java.lang.RuntimeException: PluginRegistrantCallback is not set.
E/AndroidRuntime( 8359): at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.startBackgroundIsolate(FlutterFirebaseMessagingService.java:157)
E/AndroidRuntime( 8359): at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.onCreate(FlutterFirebaseMessagingService.java:77)
E/AndroidRuntime( 8359): at android.app.ActivityThread.handleCreateService(ActivityThread.java:4192)
E/AndroidRuntime( 8359): ... 8 more
I/Process ( 8359): Sending signal. PID: 8359 SIG: 9
创建新的应用程序类
class Application : FlutterApplication(), PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
FlutterFirebaseMessagingService.setPluginRegistrant(this)
}
override fun registerWith(registry: PluginRegistry?) {
io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
}
}
改变你的Manifest
<application
android:name=".Application"
如果您正在使用Flutter Android embedded V2 (Flutter Version>= 1.12),则不需要额外的集成步骤。但是对于V1,你需要编辑。
应用程序类:
// ...
import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingBackgroundService;
public class Application extends FlutterApplication implements
PluginRegistrantCallback {
// ...
@Override
public void onCreate() {
super.onCreate();
FlutterFirebaseMessagingBackgroundService
.setPluginRegistrant(this);
}
@Override
public void registerWith(PluginRegistry registry) {
GeneratedPluginRegistrant.registerWith(registry);
}
// ...
}
Android Manifest.xml:
<application
android:name=".Application"
...
官方文档链接。