如何使用 FCM 在 Flutter 中实现推送通知?我已关注博客 https://blog.logrocket.com/flutter-push-notifications-with-firebase-cloud-messaging/ 以实现它。
void registerNotification() async {
// Initialize the Firebase app
await Firebase.initializeApp();
_messaging.requestNotificationPermissions();
_messaging.configure(
onBackgroundMessage: _firebaseMessagingBackgroundHandler,
onLaunch: (message) async {
print('onLaunch : $message');
return;
},
onMessage: (message) async {
print('OnMessage : $message');
return;
},
onResume: (message) async {
print('OnResume : $message');
return;
});
// To get Token
_messaging.getToken().then((token) {
print('Token: $token');
}).catchError((e) {
print(e);
});
}
推送通知工作很简单,我可以在后台收到通知。应用终止时通知不可用。之后,当我尝试发送推送通知时,应用程序关闭。
E/AndroidRuntime(16528): FATAL EXCEPTION: main
E/AndroidRuntime(16528): Process: com.gift.android, PID: 16528
E/AndroidRuntime(16528): java.lang.RuntimeException: Unable to create service io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService:
java.lang.RuntimeException: PluginRegistrantCallback is not set.
E/AndroidRuntime(16528): at android.app.ActivityThread.handleCreateService(ActivityThread.java:4026)
E/AndroidRuntime(16528): at android.app.ActivityThread.access$1600(ActivityThread.java:227)
E/AndroidRuntime(16528): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1906)
E/AndroidRuntime(16528): at android.os.Handler.dispatchMessage(Handler.java:107)
E/AndroidRuntime(16528): at android.os.Looper.loop(Looper.java:224)
E/AndroidRuntime(16528): at android.app.ActivityThread.main(ActivityThread.java:7592)
E/AndroidRuntime(16528): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(16528): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
E/AndroidRuntime(16528): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
E/AndroidRuntime(16528): Caused by: java.lang.RuntimeException: PluginRegistrantCallback is not set.
E/AndroidRuntime(16528): at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.startBackgroundIsolate(FlutterFirebaseMessagingService.java:157)
E/AndroidRuntime(16528): at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.onCreate(FlutterFirebaseMessagingService.java:77)
E/AndroidRuntime(16528): at android.app.ActivityThread.handleCreateService(ActivityThread.java:4014)
E/AndroidRuntime(16528): ... 8 more
I/Process (16528): Sending signal. PID: 16528 SIG: 9
Lost connection to device.
是什么导致了这个错误?如何正确实现推送通知?任何建议都会有所帮助..