firebase / flutterfire

🔥 A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.68k stars 3.97k forks source link

[[firebase_messaging]]: Error when receive message from cloud function with flutter web #12600

Closed huongtran84 closed 6 months ago

huongtran84 commented 6 months ago

Is there an existing issue for this?

Which plugins are affected?

Messaging

Which platforms are affected?

Web

Description

I expected notification showed but when cloud function send message after error fired on web

Reproducing the issue

when cloud function send message after error fired on web

Firebase Core version

2.28.0

Flutter Version

3.19.3

Relevant Log Output

[ERROR] dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 297:3       throw_
dart-sdk/lib/_internal/js_shared/lib/rti.dart 1385:3                              _failedAsCheck
dart-sdk/lib/_internal/js_shared/lib/rti.dart 1363:3                              _generalAsCheckImplementation
dart-sdk/lib/_internal/js_shared/lib/js_util_patch.dart 81:5                      getProperty
packages/firebase_messaging_web/src/interop/messaging.dart 124:16                 get data
packages/firebase_messaging_web/src/utils.dart 50:21                              messagePayloadToMap
packages/firebase_messaging_web/firebase_messaging_web.dart 35:41                 <fn>
dart-sdk/lib/async/zone.dart 1415:13                                              _rootRunUnary
dart-sdk/lib/async/zone.dart 1308:19                                              runUnary
dart-sdk/lib/async/zone.dart 1217:7                                               runUnaryGuarded
dart-sdk/lib/async/stream_impl.dart 339:5                                         [_sendData]
dart-sdk/lib/async/stream_impl.dart 271:7                                         [_add]
dart-sdk/lib/async/broadcast_stream_controller.dart 377:24                        [_sendData]
dart-sdk/lib/async/broadcast_stream_controller.dart 244:5                         add
packages/firebase_messaging_web/src/interop/messaging.dart 83:20                  <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 550:37  _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 555:39  dcall
https://www.gstatic.com/firebasejs/10.8.1/firebase-messaging.js 1:26460           messageEventListener
https://www.gstatic.com/firebasejs/10.8.1/firebase-messaging.js 1:26654

Flutter dependencies

Expand firebase_core: ^2.28.0 firebase_core_web: ^2.12.0 firebase_crashlytics: ^3.0.9 firebase_messaging: ^14.8.0 snippet
```yaml Dart SDK 3.3.3 Flutter SDK 3.19.5 vinasafe 1.0.0+1 dependencies: - animate_do 3.3.4 [flutter] - animations 2.0.11 [flutter] - build_runner 2.4.8 [analyzer args async build build_config build_daemon build_resolvers build_runner_core code_builder collection crypto dart_style frontend_server_client glob graphs http_multi_server io js logging meta mime package_config path pool pub_semver pubspec_parse shelf shelf_web_socket stack_trace stream_transform timing watcher web_socket_channel yaml] - cloud_firestore 4.16.0 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta] - cloud_functions 4.7.0 [cloud_functions_platform_interface cloud_functions_web firebase_core firebase_core_platform_interface flutter] - collection 1.18.0 - cupertino_icons 1.0.6 - darq 1.2.1 - desktop_window 0.4.0 [flutter] - dotted_border 2.1.0 [flutter path_drawing] - dropdown_button2 2.3.9 [flutter meta] - email_validator 2.1.17 - equatable 2.0.5 [collection meta] - excel 4.0.2 [archive xml collection equatable] - expandable 5.0.1 [flutter] - ffi 2.1.2 - file_picker 5.5.0 [flutter flutter_web_plugins flutter_plugin_android_lifecycle plugin_platform_interface ffi path win32] - file_selector 0.9.5 [file_selector_android file_selector_ios file_selector_linux file_selector_macos file_selector_platform_interface file_selector_web file_selector_windows flutter] - firebase_auth 4.19.0 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta] - firebase_core 2.28.0 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_core_web 2.14.0 [firebase_core_platform_interface flutter flutter_web_plugins meta web] - firebase_crashlytics 3.4.19 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace] - firebase_messaging 14.8.0 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta] - firebase_storage 11.7.0 [firebase_core firebase_core_platform_interface firebase_storage_platform_interface firebase_storage_web flutter] - flextras 1.0.0 [flutter] - flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine] - flutter_form_builder 9.2.1 [flutter intl] - flutter_localizations 0.0.0 [flutter intl characters clock collection material_color_utilities meta path vector_math] - flutter_native_splash 2.3.10 [args flutter flutter_web_plugins js html image meta path universal_io xml yaml ansicolor] - flutter_secure_storage 9.0.0 [flutter flutter_secure_storage_linux flutter_secure_storage_macos flutter_secure_storage_platform_interface flutter_secure_storage_web flutter_secure_storage_windows meta] - flutter_staggered_grid_view 0.6.2 [flutter] - flutter_svg 2.0.9 [flutter vector_graphics vector_graphics_codec vector_graphics_compiler] - flutter_treeview 1.0.7+1 [flutter] - flutter_typeahead 4.8.0 [flutter flutter_keyboard_visibility pointer_interceptor] - font_awesome_flutter 10.7.0 [flutter] - form_builder_extra_fields 10.1.0 [flutter flutter_form_builder flutter_colorpicker dropdown_search signature intl flutter_typeahead flutter_rating_bar] - form_builder_validators 9.1.0 [flutter flutter_localizations intl] - freezed 2.4.7 [analyzer build build_config collection meta source_gen freezed_annotation json_annotation] - freezed_annotation 2.4.1 [collection json_annotation meta] - http 0.13.6 [async http_parser meta] - image_picker 0.8.9 [flutter image_picker_android image_picker_for_web image_picker_ios image_picker_linux image_picker_macos image_picker_platform_interface image_picker_windows] - infinite_scroll_pagination 3.2.0 [flutter sliver_tools] - intl 0.18.1 [clock meta path] - json_annotation 4.8.1 [meta] - json_serializable 6.7.1 [analyzer async build build_config collection json_annotation meta path pub_semver pubspec_parse source_gen source_helper] - material_symbols_icons 4.2719.3 [flutter] - meilisearch 0.15.0 [dio crypto collection json_annotation meta] - modal_bottom_sheet 3.0.0 [flutter] - multi_select_flutter 4.1.3 [flutter collection] - number_inc_dec 0.8.0 [flutter] - path_provider 2.1.2 [flutter path_provider_android path_provider_foundation path_provider_linux path_provider_platform_interface path_provider_windows] - pdf 3.10.8 [archive barcode bidi crypto image meta path_parsing vector_math xml] - pluto_grid 8.0.0 [flutter intl rxdart collection] - pluto_menu_bar 3.0.1 [flutter] - printing 5.12.0 [ffi flutter flutter_web_plugins http image js meta pdf pdf_widget_wrapper plugin_platform_interface] - provider 6.1.2 [collection flutter nested] - rflutter_alert 2.0.7 [flutter] - share_plus 7.2.2 [cross_file meta mime flutter flutter_web_plugins share_plus_platform_interface file url_launcher_web url_launcher_windows url_launcher_linux url_launcher_platform_interface ffi win32] - shared_preferences 2.2.2 [flutter shared_preferences_android shared_preferences_foundation shared_preferences_linux shared_preferences_platform_interface shared_preferences_web shared_preferences_windows] - sized_context 1.0.0+4 [flutter universal_platform] - spreadsheet_decoder 2.2.0 [archive xml] - styled_widget 0.4.1 [flutter lint] - syncfusion_flutter_datagrid 23.2.7 [flutter syncfusion_flutter_core collection] - syncfusion_flutter_xlsio 23.2.7 [flutter xml archive image intl crypto jiffy syncfusion_officecore] - syncfusion_localizations 23.2.7 [flutter intl syncfusion_flutter_core] - system_info 1.0.1 [file_utils path] - textstyle_extensions 2.0.0-nullsafety [flutter] - tiengviet 0.5.0 - time 2.1.4 [clock] - tuple 2.0.2 - universal_html 2.2.4 [async csslib charcode collection html meta source_span typed_data universal_io] - universal_platform 1.0.0+1 - url_launcher 6.2.5 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows] - uuid 3.0.7 [crypto] dev dependencies: - flutter_lints 2.0.3 [lints] - flutter_test 0.0.0 [flutter test_api matcher path fake_async clock stack_trace vector_math leak_tracker_flutter_testing async boolean_selector characters collection leak_tracker leak_tracker_testing material_color_utilities meta source_span stream_channel string_scanner term_glyph vm_service] dependency overrides: - intl 0.18.1 [clock meta path] transitive dependencies: - _fe_analyzer_shared 67.0.0 [meta] - _flutterfire_internals 1.3.28 [collection firebase_core firebase_core_platform_interface flutter meta] - analyzer 6.4.1 [_fe_analyzer_shared collection convert crypto glob meta package_config path pub_semver source_span watcher yaml] - ansicolor 2.0.2 - archive 3.4.10 [crypto path pointycastle] - args 2.4.2 - async 2.11.0 [collection meta] - barcode 2.2.6 [meta qr] - bidi 2.0.10 - boolean_selector 2.1.1 [source_span string_scanner] - build 2.4.1 [analyzer async convert crypto glob logging meta package_config path] - build_config 1.1.1 [checked_yaml json_annotation path pubspec_parse yaml] - build_daemon 4.0.1 [built_collection built_value crypto http_multi_server logging path pool shelf shelf_web_socket stream_transform watcher web_socket_channel] - build_resolvers 2.4.2 [analyzer async build collection convert crypto graphs logging package_config path pool pub_semver stream_transform yaml] - build_runner_core 7.3.0 [async build build_config build_resolvers collection convert crypto glob graphs json_annotation logging meta package_config path pool timing watcher yaml] - built_collection 5.1.1 - built_value 8.9.1 [built_collection collection fixnum meta] - characters 1.3.0 - charcode 1.3.1 - checked_yaml 2.0.3 [json_annotation source_span yaml] - clock 1.1.1 - cloud_firestore_platform_interface 6.1.12 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface] - cloud_firestore_web 3.11.0 [_flutterfire_internals cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins] - cloud_functions_platform_interface 5.5.22 [firebase_core flutter meta plugin_platform_interface] - cloud_functions_web 4.9.0 [cloud_functions_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins] - code_builder 4.10.0 [built_collection built_value collection matcher meta] - convert 3.1.1 [typed_data] - cross_file 0.3.4+1 [meta web] - crypto 3.0.3 [typed_data] - csslib 1.0.0 [source_span] - dart_style 2.3.6 [analyzer args collection path pub_semver source_span] - dio 5.4.1 [async http_parser meta path] - dropdown_search 5.0.6 [flutter] - fake_async 1.3.1 [clock collection] - file 7.0.0 [meta path] - file_selector_android 0.5.0+7 [file_selector_platform_interface flutter plugin_platform_interface] - file_selector_ios 0.5.1+8 [file_selector_platform_interface flutter] - file_selector_linux 0.9.2+1 [cross_file file_selector_platform_interface flutter] - file_selector_macos 0.9.3+3 [cross_file file_selector_platform_interface flutter] - file_selector_platform_interface 2.6.2 [cross_file flutter http plugin_platform_interface] - file_selector_web 0.9.4+1 [file_selector_platform_interface flutter flutter_web_plugins web] - file_selector_windows 0.9.3+1 [cross_file file_selector_platform_interface flutter] - file_utils 1.0.1 [globbing path] - firebase_auth_platform_interface 7.2.1 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface] - firebase_auth_web 5.11.0 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser meta web] - firebase_core_platform_interface 5.0.0 [collection flutter flutter_test meta plugin_platform_interface] - firebase_crashlytics_platform_interface 3.6.26 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface] - firebase_messaging_platform_interface 4.5.30 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface] - firebase_messaging_web 3.8.0 [_flutterfire_internals firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins meta web] - firebase_storage_platform_interface 5.1.15 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface] - firebase_storage_web 3.9.0 [_flutterfire_internals async firebase_core firebase_core_web firebase_storage_platform_interface flutter flutter_web_plugins http meta web] - fixnum 1.1.0 - flutter_colorpicker 1.0.3 [flutter] - flutter_keyboard_visibility 5.4.1 [meta flutter_keyboard_visibility_platform_interface flutter_keyboard_visibility_linux flutter_keyboard_visibility_macos flutter_keyboard_visibility_web flutter_keyboard_visibility_windows flutter] - flutter_keyboard_visibility_linux 1.0.0 [flutter_keyboard_visibility_platform_interface flutter] - flutter_keyboard_visibility_macos 1.0.0 [flutter_keyboard_visibility_platform_interface flutter] - flutter_keyboard_visibility_platform_interface 2.0.0 [flutter meta plugin_platform_interface] - flutter_keyboard_visibility_web 2.0.0 [flutter_keyboard_visibility_platform_interface flutter_web_plugins flutter] - flutter_keyboard_visibility_windows 1.0.0 [flutter_keyboard_visibility_platform_interface flutter] - flutter_plugin_android_lifecycle 2.0.17 [flutter] - flutter_rating_bar 4.0.1 [flutter] - flutter_secure_storage_linux 1.2.0 [flutter flutter_secure_storage_platform_interface] - flutter_secure_storage_macos 3.0.1 [flutter flutter_secure_storage_platform_interface] - flutter_secure_storage_platform_interface 1.0.2 [flutter plugin_platform_interface] - flutter_secure_storage_web 1.1.2 [flutter flutter_secure_storage_platform_interface flutter_web_plugins js] - flutter_secure_storage_windows 3.0.0 [ffi flutter flutter_secure_storage_platform_interface path path_provider win32] - flutter_web_plugins 0.0.0 [flutter characters collection material_color_utilities meta vector_math] - frontend_server_client 3.2.0 [async path] - glob 2.1.2 [async collection file path string_scanner] - globbing 1.0.0 [path] - graphs 2.3.1 [collection] - html 0.15.4 [csslib source_span] - http_multi_server 3.2.1 [async] - http_parser 4.0.2 [collection source_span string_scanner typed_data] - image 4.1.7 [archive meta xml] - image_picker_android 0.8.9+3 [flutter flutter_plugin_android_lifecycle image_picker_platform_interface] - image_picker_for_web 2.2.0 [flutter flutter_web_plugins image_picker_platform_interface mime] - image_picker_ios 0.8.9+2 [flutter image_picker_platform_interface] - image_picker_linux 0.2.1+1 [file_selector_linux file_selector_platform_interface flutter image_picker_platform_interface] - image_picker_macos 0.2.1+1 [file_selector_macos file_selector_platform_interface flutter image_picker_platform_interface] - image_picker_platform_interface 2.9.4 [cross_file flutter http plugin_platform_interface] - image_picker_windows 0.2.1+1 [file_selector_platform_interface file_selector_windows flutter image_picker_platform_interface] - io 1.0.4 [meta path string_scanner] - jiffy 6.2.2 [intl] - js 0.6.7 [meta] - leak_tracker 10.0.0 [clock collection meta path vm_service] - leak_tracker_flutter_testing 2.0.1 [flutter leak_tracker leak_tracker_testing matcher meta] - leak_tracker_testing 2.0.1 [leak_tracker matcher meta] - lint 1.10.0 - lints 2.1.1 - logging 1.2.0 - matcher 0.12.16+1 [async meta stack_trace term_glyph test_api] - material_color_utilities 0.8.0 [collection] - meta 1.11.0 - mime 1.0.5 - nested 1.0.0 [flutter] - package_config 2.1.0 [path] - path 1.9.0 - path_drawing 1.0.1 [vector_math meta path_parsing flutter] - path_parsing 1.0.1 [vector_math meta] - path_provider_android 2.2.2 [flutter path_provider_platform_interface] - path_provider_foundation 2.3.2 [flutter path_provider_platform_interface] - path_provider_linux 2.2.1 [ffi flutter path path_provider_platform_interface xdg_directories] - path_provider_platform_interface 2.1.2 [flutter platform plugin_platform_interface] - path_provider_windows 2.2.1 [ffi flutter path path_provider_platform_interface win32] - pdf_widget_wrapper 1.0.3 [flutter pdf] - petitparser 6.0.2 [meta] - platform 3.1.4 - plugin_platform_interface 2.1.8 [meta] - pointer_interceptor 0.9.3+7 [flutter] - pointycastle 3.7.4 [collection convert js] - pool 1.5.1 [async stack_trace] - pub_semver 2.1.4 [collection meta] - pubspec_parse 1.2.3 [checked_yaml collection json_annotation pub_semver yaml] - qr 3.0.1 [meta] - rxdart 0.27.7 - share_plus_platform_interface 3.3.1 [cross_file flutter meta mime plugin_platform_interface path_provider uuid] - shared_preferences_android 2.2.1 [flutter shared_preferences_platform_interface] - shared_preferences_foundation 2.3.5 [flutter shared_preferences_platform_interface] - shared_preferences_linux 2.3.2 [file flutter path path_provider_linux path_provider_platform_interface shared_preferences_platform_interface] - shared_preferences_platform_interface 2.3.2 [flutter plugin_platform_interface] - shared_preferences_web 2.3.0 [flutter flutter_web_plugins shared_preferences_platform_interface web] - shared_preferences_windows 2.3.2 [file flutter path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface] - shelf 1.4.1 [async collection http_parser path stack_trace stream_channel] - shelf_web_socket 1.0.4 [shelf stream_channel web_socket_channel] - signature 5.4.1 [flutter flutter_svg image] - sky_engine 0.0.99 - sliver_tools 0.2.12 [flutter] - source_gen 1.5.0 [analyzer async build dart_style glob path source_span yaml] - source_helper 1.3.4 [analyzer collection source_gen] - source_span 1.10.0 [collection path term_glyph] - stack_trace 1.11.1 [path] - stream_channel 2.1.2 [async] - stream_transform 2.1.0 - string_scanner 1.2.0 [source_span] - syncfusion_flutter_core 23.2.7 [vector_math flutter] - syncfusion_officecore 23.2.7 [flutter syncfusion_flutter_core] - term_glyph 1.2.1 - test_api 0.6.1 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph] - timing 1.0.1 [json_annotation] - typed_data 1.3.2 [collection] - universal_io 2.2.2 [collection meta typed_data] - url_launcher_android 6.3.0 [flutter url_launcher_platform_interface] - url_launcher_ios 6.2.5 [flutter url_launcher_platform_interface] - url_launcher_linux 3.1.1 [flutter url_launcher_platform_interface] - url_launcher_macos 3.1.0 [flutter url_launcher_platform_interface] - url_launcher_platform_interface 2.3.2 [flutter plugin_platform_interface] - url_launcher_web 2.3.0 [flutter flutter_web_plugins url_launcher_platform_interface web] - url_launcher_windows 3.1.1 [flutter url_launcher_platform_interface] - vector_graphics 1.1.10+1 [flutter vector_graphics_codec] - vector_graphics_codec 1.1.10+1 - vector_graphics_compiler 1.1.10+1 [args meta path_parsing xml vector_graphics_codec path] - vector_math 2.1.4 - vm_service 13.0.0 - watcher 1.1.0 [async path] - web 0.5.1 - web_socket_channel 2.4.4 [async crypto stream_channel web] - win32 5.3.0 [ffi] - xdg_directories 1.0.4 [meta path] - xml 6.5.0 [collection meta petitparser] - yaml 3.1.2 [collection source_span string_scanner] ```

Additional context and comments

No response

russellwheatley commented 6 months ago

Hi @huongtran84 - could you show us how you're sending a message via cloud function? What state is the app in (foreground, background or terminated)?

Steps on reproducing the bug would be helpful. Thanks.

huongtran84 commented 6 months ago

Hi @huongtran84 - could you show us how you're sending a message via cloud function? What state is the app in (foreground, background or terminated)?

Steps on reproducing the bug would be helpful. Thanks.

I use following function to sendMessage : // Function to send FCM notifications to client app async function sendNotificationToClient(fcmToken,fileName, status) {

try {
     const payload = {
    notification: {
      title:'Đang xử lý tài liệu ',
      body: `Tài liệu ${fileName} ${status}.`,
    },
    // 3
    token: fcmToken,
  }
    // Gửi thông báo đến FCM token
    await admin.messaging().send(payload);

console.log('FCM notification sent to client.');

return { success: true };

} catch (error) { console.error('Lỗi gửi thông báo:', error); throw new functions.https.HttpsError('internal', 'Lỗi gửi thông báo'); } } this function lay in a onCall funtion that I sent to it fcm token from client. I want receive notification in foreground state.

` @override void initState() { /// Get a reference to the app model appModel = context.read(); chitietCTModel = context.read(); initData();

getPermission();
messageListener(context);
Future.microtask(() => trySetCurrentPage(
    UniversalPlatform.isWeb ? PageType.doituong : PageType.hosokt, false));
super.initState();

}

Future getPermission() async { FirebaseMessaging messaging = FirebaseMessaging.instance; final fcmToken = await FirebaseMessaging.instance.getToken( vapidKey: "BCXrrngUAPwEfxrWCWjwz3m7jrPy1RiTaGSylZ9v1NYhKAIotbAjs7koxweC2HgxEamDbScwDS7tCmcKe1VgFes"); await setToken(fcmToken); FirebaseMessaging.instance.onTokenRefresh.listen(setToken);

NotificationSettings settings = await messaging.requestPermission(
  alert: true,
  announcement: false,
  badge: true,
  carPlay: false,
  criticalAlert: false,
  provisional: false,
  sound: true,
);

print('User granted permission: ${settings.authorizationStatus}');

} void messageListener(BuildContext context) { FirebaseMessaging.onMessage.listen((RemoteMessage message) { print('Got a message whilst in the foreground!'); print('Message data: ${message.data}');

  if (message.notification != null) {
    print(
        'Message also contained a notification: ${message.notification?.body}');
    showDialog(
        context: context,
        builder: ((BuildContext context) {
          return DynamicDialog(
              title: message.notification?.title,
              body: message.notification?.body);
        }));
  }
});

}

`

russellwheatley commented 6 months ago

Thanks, I was able to reproduce.