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.61k stars 3.95k forks source link

[firebase_messaging]: FirebaseMessaging.onMessage.listen fired twice only in IOS (14.7.21) #12544

Closed karaaslan18 closed 4 months ago

karaaslan18 commented 5 months ago

Is there an existing issue for this?

Which plugins are affected?

Messaging

Which platforms are affected?

iOS

Description

FirebaseMessaging.onMessage.listen fires twice only in IOS, Android works fine.

When I send one notification it called twice and printed:

[log] on message: 1711642425172749 ... [log] {title: Test, body: Test body 2024-03-28T19:13:44.654474} [log] on message: 1711642425172749 ... [log] {title: Test, body: Test body 2024-03-28T19:13:44.654474}

Reproducing the issue

FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
      log("on message: ${message.messageId} ...");
      final notification = message.data;
      log("$notification");
      if (notification["title"] != null && notification["body"] != null) {
        final ln = LocalNotification.n(title: notification["title"], body: notification["body"]);

        await IsarHandler.instance.addNotification(ln);
        await LocalNotificationsHandler.pushNotification(ln);

        if (await FlutterAppBadger.isAppBadgeSupported()) {
          final count = await IsarHandler.instance.getUnreadNotificationCount();
          await FlutterAppBadger.updateBadgeCount(count);
        }
      }
    });

Sent notification via cloud function

exports.sendNotification = functions.https.onCall( async (data) => {
  // extract request data
  const tokens = data.tokens;
  const title = data.title;
  const body = data.body;

  // send message
  for (const token of tokens) {
    const message = {
      "data": {"title": title, "body": body},
      "apns": {"payload": {
        "aps": {"content-available": true, "sound": "default"},
      }},
      "token": token,
    };
    console.log(message);
    try {
      await admin.messaging().send(message);
      console.log("Message sent successfully:", token);
    } catch (error) {
      console.error("Error sending message:");
      console.error(error);
    }
  }
});

Firebase Core version

2.27.2

Flutter Version

3.19.2

Relevant Log Output

[log] on message: 1711642425172749 ...
[log] {title: Test, body: Test body 2024-03-28T19:13:44.654474}
[log] on message: 1711642425172749 ...
[log] {title: Test, body: Test body 2024-03-28T19:13:44.654474}

Flutter dependencies

Expand Flutter dependencies snippet
```yaml - adaptive_action_sheet 2.0.3 [flutter universal_io] - calendar_view 1.1.0 [flutter] - cloud_firestore 4.15.8 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta] - cloud_functions 4.6.8 [cloud_functions_platform_interface cloud_functions_web firebase_core firebase_core_platform_interface flutter] - collection 1.18.0 - connecteo 2.2.0 [connectivity_plus flutter http rxdart] - cupertino_icons 1.0.6 - device_info_plus 9.1.2 [device_info_plus_platform_interface ffi file flutter flutter_web_plugins meta win32 win32_registry] - dio 5.4.2+1 [async http_parser meta path] - excel 4.0.3 [archive xml collection equatable] - ffmpeg_kit_flutter_full 6.0.3 [ffmpeg_kit_flutter_platform_interface flutter] - file_picker 6.2.1 [flutter flutter_web_plugins flutter_plugin_android_lifecycle plugin_platform_interface ffi path win32] - firebase_auth 4.17.8 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta] - firebase_core 2.27.2 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_messaging 14.7.21 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta] - firebase_storage 11.6.9 [firebase_core firebase_core_platform_interface firebase_storage_platform_interface firebase_storage_web flutter] - flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine] - flutter_app_badger 1.5.0 [flutter] - flutter_file_dialog 3.0.2 [flutter] - flutter_image_compress 2.2.0 [flutter flutter_image_compress_platform_interface flutter_image_compress_common flutter_image_compress_web flutter_image_compress_macos flutter_image_compress_ohos] - flutter_local_notifications 16.3.3 [clock flutter flutter_local_notifications_linux flutter_local_notifications_platform_interface timezone] - flutter_localizations 0.0.0 [flutter intl characters clock collection material_color_utilities meta path vector_math] - flutter_native_splash 2.4.0 [args flutter flutter_web_plugins html image meta path universal_io xml yaml ansicolor] - flutter_pdfview 1.3.2 [flutter] - flutter_slidable 3.1.0 [flutter] - flutter_speed_dial 7.0.0 [flutter] - flutter_svg 2.0.10+1 [flutter http vector_graphics vector_graphics_codec vector_graphics_compiler] - font_awesome_flutter 10.7.0 [flutter] - gal 2.3.0 [flutter] - geocoding 2.2.2 [flutter geocoding_platform_interface geocoding_android geocoding_ios] - geoflutterfire2 2.3.15 [flutter cloud_firestore rxdart] - geolocator 10.1.1 [flutter geolocator_platform_interface geolocator_android geolocator_apple geolocator_web geolocator_windows] - google_maps_flutter 2.5.3 [flutter google_maps_flutter_android google_maps_flutter_ios google_maps_flutter_platform_interface google_maps_flutter_web] - http 1.2.1 [async http_parser meta web] - image_picker 1.0.7 [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 4.0.0 [flutter flutter_staggered_grid_view sliver_tools] - intl 0.18.1 [clock meta path] - isar 3.1.0+1 [ffi js meta] - isar_flutter_libs 3.1.0+1 [flutter isar] - lottie 2.7.0 [archive flutter path vector_math] - map_launcher 3.2.0 [flutter] - modal_bottom_sheet 3.0.0 [flutter] - package_info_plus 6.0.0 [ffi flutter flutter_web_plugins http meta path package_info_plus_platform_interface web win32] - 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] - pdf_merger 0.0.6 [flutter get] - percent_indicator 4.2.3 [flutter] - permission_handler 11.3.1 [flutter meta permission_handler_android permission_handler_apple permission_handler_html permission_handler_windows permission_handler_platform_interface] - phone_form_field 8.1.1 [flutter flutter_localizations circle_flags phone_numbers_parser intl diacritic] - photo_view 0.14.0 [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] - timeline_tile 2.0.0 [flutter] - upgrader 10.0.1 [flutter device_info_plus html http os_detect package_info_plus shared_preferences url_launcher version xml] - 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: - 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] - flutter_launcher_icons 0.13.1 [args checked_yaml cli_util image json_annotation path yaml] - flutter_lints 3.0.2 [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] - isar_generator 3.1.0+1 [analyzer build dart_style dartx glob isar path source_gen xxh3] transitive dependencies: - _fe_analyzer_shared 61.0.0 [meta] - _flutterfire_internals 1.3.27 [collection firebase_core firebase_core_platform_interface flutter meta] - analyzer 5.13.0 [_fe_analyzer_shared collection convert crypto glob meta package_config path pub_semver source_span watcher yaml] - ansicolor 2.0.2 - archive 3.4.9 [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 - checked_yaml 2.0.3 [json_annotation source_span yaml] - circle_flags 3.0.1 [flutter flutter_svg] - cli_util 0.4.1 [meta path] - clock 1.1.1 - cloud_firestore_platform_interface 6.1.9 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface] - cloud_firestore_web 3.10.8 [_flutterfire_internals cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins js] - cloud_functions_platform_interface 5.5.21 [firebase_core flutter meta plugin_platform_interface] - cloud_functions_web 4.8.1 [cloud_functions_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js web] - code_builder 4.10.0 [built_collection built_value collection matcher meta] - connectivity_plus 5.0.2 [flutter flutter_web_plugins connectivity_plus_platform_interface js meta nm] - connectivity_plus_platform_interface 1.2.4 [flutter meta plugin_platform_interface] - 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.2 [analyzer args path pub_semver source_span] - dartx 1.2.0 [characters collection crypto meta path time] - dbus 0.7.10 [args ffi meta xml] - device_info_plus_platform_interface 7.0.0 [flutter meta plugin_platform_interface] - diacritic 0.1.5 - equatable 2.0.5 [collection meta] - fake_async 1.3.1 [clock collection] - ffi 2.1.2 - ffmpeg_kit_flutter_platform_interface 0.2.1 [flutter plugin_platform_interface] - file 7.0.0 [meta path] - 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_windows 0.9.3+1 [cross_file file_selector_platform_interface flutter] - firebase_auth_platform_interface 7.2.0 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface] - firebase_auth_web 5.10.1 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser js meta web] - firebase_core_platform_interface 5.0.0 [collection flutter flutter_test meta plugin_platform_interface] - firebase_core_web 2.13.0 [firebase_core_platform_interface flutter flutter_web_plugins js meta web] - firebase_messaging_platform_interface 4.5.29 [_flutterfire_internals firebase_core flutter meta plugin_platform_interface] - firebase_messaging_web 3.7.1 [_flutterfire_internals firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta web] - firebase_storage_platform_interface 5.1.14 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface] - firebase_storage_web 3.8.1 [_flutterfire_internals async firebase_core firebase_core_web firebase_storage_platform_interface flutter flutter_web_plugins http js meta web] - fixnum 1.1.0 - flutter_image_compress_common 1.0.5 [flutter flutter_image_compress_platform_interface] - flutter_image_compress_macos 1.0.2 [flutter flutter_image_compress_platform_interface] - flutter_image_compress_ohos 0.0.2 [flutter flutter_image_compress_platform_interface] - flutter_image_compress_platform_interface 1.0.5 [flutter plugin_platform_interface cross_file] - flutter_image_compress_web 0.1.4+1 [flutter flutter_web_plugins flutter_image_compress_platform_interface js] - flutter_local_notifications_linux 4.0.0+1 [dbus ffi flutter flutter_local_notifications_platform_interface path xdg_directories] - flutter_local_notifications_platform_interface 7.0.0+1 [flutter plugin_platform_interface] - flutter_plugin_android_lifecycle 2.0.17 [flutter] - flutter_staggered_grid_view 0.7.0 [flutter] - flutter_web_plugins 0.0.0 [flutter characters collection material_color_utilities meta vector_math] - frontend_server_client 3.2.0 [async path] - geocoding_android 3.3.0 [flutter geocoding_platform_interface] - geocoding_ios 2.3.0 [flutter geocoding_platform_interface] - geocoding_platform_interface 3.2.0 [flutter meta plugin_platform_interface] - geolocator_android 4.3.1 [flutter geolocator_platform_interface uuid] - geolocator_apple 2.3.7 [flutter geolocator_platform_interface] - geolocator_platform_interface 4.2.2 [flutter plugin_platform_interface vector_math meta] - geolocator_web 2.2.1 [flutter flutter_web_plugins geolocator_platform_interface] - geolocator_windows 0.2.3 [flutter geolocator_platform_interface] - get 4.6.6 [flutter] - glob 2.1.2 [async collection file path string_scanner] - google_maps 6.3.0 [js js_wrapping meta] - google_maps_flutter_android 2.7.0 [flutter flutter_plugin_android_lifecycle google_maps_flutter_platform_interface stream_transform] - google_maps_flutter_ios 2.5.2 [flutter google_maps_flutter_platform_interface stream_transform] - google_maps_flutter_platform_interface 2.6.0 [collection flutter plugin_platform_interface stream_transform] - google_maps_flutter_web 0.5.4+3 [collection flutter flutter_web_plugins google_maps google_maps_flutter_platform_interface sanitize_html stream_transform] - 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 3.0.2 [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] - js 0.6.7 [meta] - js_wrapping 0.7.4 [js] - json_annotation 4.8.1 [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] - lints 3.0.0 - 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 - nm 0.5.0 [dbus] - os_detect 2.0.1 - package_config 2.1.0 [path] - package_info_plus_platform_interface 2.0.1 [flutter meta plugin_platform_interface] - path 1.9.0 - 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] - permission_handler_android 12.0.5 [flutter permission_handler_platform_interface] - permission_handler_apple 9.4.4 [flutter permission_handler_platform_interface] - permission_handler_html 0.1.1 [flutter flutter_web_plugins permission_handler_platform_interface] - permission_handler_platform_interface 4.2.1 [flutter meta plugin_platform_interface] - permission_handler_windows 0.2.1 [flutter permission_handler_platform_interface] - petitparser 6.0.2 [meta] - phone_numbers_parser 8.2.1 [meta] - platform 3.1.4 - plugin_platform_interface 2.1.8 [meta] - 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 - sanitize_html 2.1.0 [html meta] - share_plus_platform_interface 3.4.0 [cross_file flutter meta mime plugin_platform_interface path_provider uuid] - 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] - 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] - 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_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] - 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] - time 2.1.4 [clock] - timezone 0.9.2 [path] - 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.11+1 [flutter http vector_graphics_codec] - vector_graphics_codec 1.1.11+1 - vector_graphics_compiler 1.1.11+1 [args meta path_parsing xml vector_graphics_codec path] - vector_math 2.1.4 - version 3.0.2 - 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] - win32_registry 1.1.2 [ffi win32] - xdg_directories 1.0.4 [meta path] - xml 6.5.0 [collection meta petitparser] - xxh3 1.0.1 - yaml 3.1.2 [collection source_span string_scanner] ```

Additional context and comments

No response

Lyokone commented 5 months ago

@karaaslan18 I couldn't reproduce on my end, could you provide a complete project to see what is happening? Thanks

karaaslan18 commented 5 months ago

While waiting I decided to change the notification sender function. Because it is said here I should not use silent notifications for IOS. So I added

"notification": {"title": title, "body": body},

then somehow it only fired once. I am not planning to remove the notification from the message because the IOS side needs it. But for testing purposes, I have tried without it multiple times. All created two notifications.

The project is too big I am not allowed to share it, sorry. I will ask the team to create a test app but since adding notification solved multiple notification problem I do not think it will be possible.