我在我的 Flutter Web 应用程序中使用了 firebase 消息传递,我可以接收和处理 sdk,并且使用此功能在前台一切正常:
import 'package:firebase/firebase.dart' as firebase;
class PushNotificationService {
firebase.Messaging _mc;
Future initialise() async {
_mc = firebase.messaging();
_mc.onMessage.listen((event) {
print('New Message: ${event.data}');
}
}
}
但是当我尝试使用此代码收听后台事件时:
_mc.onBackgroundMessage.listen((event) {
print('New Message: ${event.data}');
});
我收到一个异常:
FirebaseError: Messaging: This method is available in a service worker context. (messaging/only-available-in-sw). at ot.setBackgroundMessageHandler (https://www.gstatic.com/firebasejs/8.0.1/firebase-messaging.js:1:38164) at messaging.Messaging._fromJsObject.[_createBackgroundMessagedStream] (http://localhost:14698/packages/firebase/src/messaging.dart.lib.js:100:23) at messaging.Messaging._fromJsObject.get onBackgroundMessage [as onBackgroundMessage] (http://localhost:14698/packages/firebase/src/messaging.dart.lib.js:76:51) at firebase_push_notification_service.PushNotificationService.new.initialise (http://localhost:14698/packages/bpazar/catalog/presentation/bestSelling/best_selling_ui.dart.lib.js:155066:19) at initialise.next (<anonymous>) at runBody (http://localhost:14698/dart_sdk.js:38027:34) at Object._async [as async] (http://localhost:14698/dart_sdk.js:38058:7) at firebase_push_notification_service.PushNotificationService.new.initialise (http://localhost:14698/packages/bpazar/catalog/presentation/bestSelling/best_selling_ui.dart.lib.js:155059:20) at setUpNotifications (http://localhost:14698/packages/bpazar/catalog/presentation/bestSelling/best_selling_ui.dart.lib.js:147880:40) at setUpNotifications.next (<anonymous>) at runBody (http://localhost:14698/dart_sdk.js:38027:34) at Object._async [as async] (http://localhost:14698/dart_sdk.js:38058:7) at home.HomeViewState.new.setUpNotifications (http://localhost:14698/packages/bpazar/catalog/presentation/bestSelling/best_selling_ui.dart.lib.js:147878:20) at home.HomeViewState.new.initState (http://localhost:14698/packages/bpazar/catalog/presentation/bestSelling/best_selling_ui.dart.lib.js:147459:12)
所以我的问题是: 有没有办法处理flutter web中的后台通知?